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