关于流式文件行读取操作

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
 //随便写点东东放该文件
 ofstream outfile;
 outfile.open("file.txt");
 if(!outfile.is_open())
 {
  cout<<"Can not open the file."<<endl;
  return 0;
 }
 else
 {
  outfile.clear();
  outfile<<"我在那个角落"<<endl<<"患过伤风"<<endl<<"I am here"<<endl;
 }
 outfile.close();
 
 //然后逐行读取它 再打印出来
 ifstream infile;
 infile.open("file.txt");
 if(!infile.is_open())
 { 
  cout<<"Can not open the file"<<endl;
  return 0;
 }
 else
 {
  string line;       //每次从文件读一行
  while(!infile.eof())
  {
   getline(infile,line);
   cout<<line<<endl;
  }
 }
 infile.close();
 system("pause");
 return 0;
}

你可能感兴趣的:(String,File,System,include)