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 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM