c++學習筆記—c++對txt文件的讀取與寫入


一、文件的輸入輸出

頭文件fstream定義了三個類型支持文件IO:ifstream從給定文件讀取數據、ofstream向一個給定文件寫入數據、fstream讀寫給定數據。這些類型與cin和cout的操作一樣,我們可以用IO操作符來讀寫文件,還可以用getline從一個ifstream讀取數據。

1、getline()函數

getline的函數原型為:

 

[cpp]  view plain copy
 
  1. istream& getline(istream&  is, string& str, char delim);  
  2. istream& getline(istream&& is, string& str, char delim);  
  3. istream& getline(istream&  is, string& str);  
  4. istream& getline(istream&& is, string& str);  

通常我們使用getline函數讀取一整行,該函數接受一個輸入流和一個string對象,函數從給定的輸入流中讀取內容,直到遇到換行符為止,然后將所讀的內容存入到個string對象中。

 

另外,當函數為istream& getline (istream& is, string& str, char delim);形式時,函數遇到delim也會停止。

2、使用文件流對象

當我們想要讀入一個文件時,可以定義一個文件流對象,並將對象與文件相關聯起來,每一個文件流類都定義了一個名為open的成員函數,完成一系列系統相關的操作。

open函數的原型為:

 

[cpp]  view plain copy
 
  1. void open (const   char* filename,  ios_base::openmode mode = ios_base::out);  
  2. void open (const string& filename,  ios_base::openmode mode = ios_base::out);  

文件模式(mode)有一下幾種:

[cpp]  view plain copy
 
  1. ofstream outfile("E:\\out.txt", ofstream::app);  

上述代碼打開out.txt文件,如果不存在,系統會創建此txt文件,並且定位到文件末尾。
打開的文件使用完成后一定要關閉,fstream提供了成員函數close()來完成此操作。

 

例:從hello.txt文件中讀取數據並寫入到out.txt中

 

[cpp]  view plain copy
 
  1. #include "stdafx.h"  
  2. #include <vector>  
  3. #include <string>  
  4. #include <fstream>  
  5. #include <iostream>  
  6. using namespace std;  
  7. int _tmain(int argc, _TCHAR* argv[])  
  8. {  
  9.     ifstream myfile("E:\\hello.txt");  
  10.     ofstream outfile("E:\\out.txt", ofstream::app);  
  11.     string temp;  
  12.     if (!myfile.is_open())  
  13.     {  
  14.         cout << "未成功打開文件" << endl;  
  15.     }  
  16.     while(getline(myfile,temp))  
  17.     {  
  18.         outfile<<temp;  
  19.     }  
  20.     myfile.close();  
  21.     return 0;  
  22. }  



 

二、string流

string頭文件定義了三個類型來支持內存IO,istringstream向string寫入數據,ostringstream從string讀取數據,stringstream既可從string讀取數據也可向string寫數據,就像string是一個IO流一樣。

1、istringstream的用法

 

[cpp]  view plain copy
 
  1. #include "stdafx.h"  
  2. #include <string>  
  3. #include <sstream>    //使用istringstream所需要的頭文件  
  4. #include <iostream>  
  5. using namespace std;  
  6. int _tmain(int argc, _TCHAR* argv[])  
  7. {  
  8.     string str = "I am a boy";  
  9.     istringstream is(str);  
  10.     string s;  
  11.     while (is >> s)  
  12.     {  
  13.         cout << s << endl;  
  14.     }  
  15.     return 0;  
  16. }  

輸出結果為:

 

I

am

a

boy

例:編寫程序,將來自一個文件中的行保存在一個vector<string>中,然后使用istringstream從vector讀取數據元素,每次讀取一個單詞。

 

[cpp]  view plain copy
 
  1. #include "stdafx.h"  
  2. #include <vector>  
  3. #include <string>  
  4. #include <fstream>  
  5. #include <sstream>  
  6. #include <iostream>  
  7. using namespace std;  
  8. int _tmain(int argc, _TCHAR* argv[])  
  9. {  
  10.     vector<string> vec;  
  11.     ifstream myfile("E:\\hello.txt");  
  12.     string temp;  
  13.     if (!myfile.is_open())  
  14.     {  
  15.         cout << "未成功打開文件" << endl;  
  16.     }  
  17.     while(getline(myfile,temp))  
  18.     {  
  19.         vec.push_back(temp);  
  20.     }  
  21.     for (auto it = vec.begin(); it != vec.end(); it++)  
  22.     {  
  23.         cout << *it << endl;  
  24.     }  
  25.     cout << "-----------------使用istringstream------------------------" << endl;  
  26.     for (auto it = vec.begin(); it != vec.end(); it++)  
  27.     {  
  28.       
  29.         istringstream record(*it);  
  30.         string s;  
  31.         while (record >> s)  
  32.             cout << s << endl;  
  33.     }  
  34.     return 0;  
  35. }  

 

運行結果如圖所示:

//下述論述轉自www.cndev-lab.com ,程序作者:管寧 

 

 

[cpp]  view plain copy
 
  1. #i nclude <iostream>   
  2. #i nclude <sstream>   
  3. using namespace std;   
  4. int main()     
  5. {   
  6.     istringstream istr;   
  7.     istr.str("1 56.7",);   
  8.     //上述兩個過程可以簡單寫成 istringstream istr("1 56.7");   
  9.     cout << istr.str()<<endl;   
  10.     int a;   
  11.     float b;   
  12.     istr>>a;   
  13.     cout<<a<<endl;   
  14.     istr>>b;   
  15.     cout<<b<<endl;   
  16.     system("pause");   
  17. }  

 

  上例中,構造字符串流的時候,空格會成為字符串參數的內部分界,例子中對a,b對象的輸入"賦值"操作證明了這一點,字符串的空格成為了整型數據與浮點型數據的分解點,利用分界獲取的方法我們事實上完成了字符串到整型對象與浮點型對象的拆分轉換過程。

  str()成員函數的使用可以讓istringstream對象返回一個string字符串(例如本例中的輸出操作(cout<<istr.str();)。

 

2、ostringstream的用法

 ostringstream同樣是由一個string對象構造而來,ostringstream類向一個string插入字符。 
 ostringstream的構造函數原形如下: 

[cpp]  view plain copy
 
  1. ostringstream::ostringstream(string str);   

 

//下述論述轉自www.cndev-lab.com ,程序作者:管寧 

 

[cpp]  view plain copy
 
  1. #i nclude <iostream>   
  2. #i nclude <sstream>   
  3. #i nclude <string>   
  4. using namespace std;   
  5. int main()     
  6. {   
  7.     ostringstream ostr;   
  8.     //ostr.str("abc");//如果構造的時候設置了字符串參數,那么增長操作的時候不會從結        尾開始增加,而是修改原有數據,超出的部分增長   
  9.     ostr.put('d');   
  10.     ostr.put('e');   
  11.     ostr<<"fg";   
  12.   
  13.     string gstr = ostr.str();   
  14.     cout<<gstr;   
  15.     system("pause");   
  16. }  

在上例代碼中,我們通過put()或者左移操作符可以不斷向ostr插入單個字符或者是字符串,通過str()函數返回增長過后的完整字符串數據,但值 得注意的一點是,當構造的時候對象內已經存在字符串數據的時候,那么增長操作的時候不會從結尾開始增加,而是修改原有數據,超出的部分增長。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM