LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1370|回复: 0

对seekg()函数的疑问

[复制链接]
发表于 2007-4-14 14:08:37 | 显示全部楼层 |阅读模式
我想用inFile.seekg(0,ios:beg)让文件指针指向文件的开头,好像没有起作用


  1. #include<iostream>
  2. #include<fstream>
  3. #include<iomanip>

  4. using namespace std;

  5. int main()
  6. {
  7.    int i=0;
  8.    int j=0;
  9.    long Primes,Primes_1;
  10.    char* filename="/C++/file/primes";
  11.    ifstream inFile(filename,ios::in|ios::binary);

  12.    if(!inFile)
  13.    cerr<<"error opening the file  primes!!"<<endl;

  14.    while(!inFile.eof())
  15.    {
  16.       i++;
  17.       inFile>>Primes_1;            
  18.     }              //已经指向末尾
  19.    i=i-1;
  20.    cout<<i<<endl;
  21.    inFile.seekg(0,ios::beg);       //想让它指到开始的地方
  22.    for(j=0;j<i;j++)
  23.    {
  24.       inFile>>Primes;
  25.       cout<<setw(10)<<Primes;
  26.       (j%5==0)?cout<<"\n":cout<<"  ";
  27.     }
  28.    cout<<endl;
  29.    inFile.close();
  30.    return 0;
  31. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表