文件的读取

作者在 2009-03-11 23:30:25 发布以下内容
文件读入读出当然是最简单的ofstream 和 ifstream,当想从文件的某个位置来读取呢?看下面代码
1.将b.txt中的数据一个一个读到a.txt
char a='0';
 ifstream fin("b.txt");
 ofstream fout;
 ifstream::pos_type cur;
 while(fin>>a)
 {
  cur=fin.tellg();
  fin.close();
  fout.open("a.txt",ios::app);
  fout<<a;
     fout<<flush;
  
  fout.close();
  fin.open("b.txt");
  fin.seekg(cur);
 }
 
2.计算文件的长度
is.seekg (0, ios::end);
  length = is.tellg();
 
根据上面两个定义,原来istream::pos_type 来表示偏移量,甚至可以和int互用,太神奇了,具体原因还在探索中。。。
 
C++ | 阅读 1655 次
文章评论,共0条
游客请输入验证码
文章分类
文章归档
最新评论