C++怎样将文件指针指向文件的首部和



#include
#include
#include

using namespace std;

int main(void)
{
 ifstream in;
 ofstream out;
 out.open("tmp.txt");
 in.open("hello.txt");
 string buf;
 int count = 0;
 while (getline(in, buf)) {
  count++;
  if (count == 4)
    in.seekg(0, ios::beg); //这里使用了seekg()来使文件指针重新定位到头部, 也可以写成in.seek(0);
  out << buf << endl;
 }

 in.close();
 out.close();

 return 0;
}

你可能感兴趣的:(学习笔记)