可以用getline函数,举个例子如下(从b.txt中按行循环读取内容):
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int cnt=0;
string str;
ifstream fin("b.txt", ios::in);
if(!fin){
printf("The file is not exist!");
return -1;
}
while(getline(fin, str))
{
cnt++;
cout<<str<<endl;
}
return 0;
}
b.txt中的内容如下:

循环读取后输出:
