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,'\n');
19     while (fin)
20     {
21         ++count;
22         cout<<count<<":"<<stringitem<<endl;
23         getline(fin,stringitem,'\n');
24     }
25     cout<<"Done"<<endl;
26 
27     fin.close();
28     return 0;
29 }

 

posted @ 2017-06-15 09:33  冥天笑  阅读(3364)  评论(0编辑  收藏  举报