getline函数按行读取文件

 1 #include <iostream>
 2 #include <string>
 3 #include <fstream>
 4 #include <cstdlib>
 5 
 6 using namespace std;
 7 int main()
 8 {
 9     ifstream fin;
10     fin.open("tobuy.txt");
11     if (fin.is_open() == false)
12     {
13         cerr<<"can not open"<<endl;
14         exit(EXIT_FAILURE);
15     }
16     string stringitem;
17     int count = 0;
18     getline(fin,stringitem,'
');
19     while (fin)
20     {
21         ++count;
22         cout<<count<<":"<<stringitem<<endl;
23         getline(fin,stringitem,'
');
24     }
25     cout<<"Done"<<endl;
26 
27     fin.close();
28     return 0;
29 }
原文地址:https://www.cnblogs.com/LYF-LIUDAO/p/7015812.html