加载中...

15.3 文件输出


将输出发送到文件的方法与处理输入类似。例如,我们可以修改前面的程序以实现将一个文件逐行复制到另一个文件的功能。

  1. ifstream infile ("input-file");
  2. ofstream outfile ("output-file");
  3. if (infile.good() == false || outfile.good() == false) {
  4. cout << "Unable to open one of the files." << endl;
  5. exit (1);
  6. }
  7. while (true) {
  8. getline (infile, line);
  9. if (infile.eof()) break;
  10. outfile << line << endl;
  11. }

还没有评论.