c++檔案流基本用法(fstream, ifstream, ostream)

來源:互聯網
上載者:User

原文連結

前言:
c++的檔案流處理其實很簡單,前提是你能夠理解它。檔案流本質是利用了一個buffer中介層。有點類似標準輸出和標準輸入一樣。

c++ IO的設計保證IO效率,同時又兼顧封裝性和易用性。本文將會講述c++檔案流的用法。

有錯誤和疏漏的地方,歡迎批評指證。

需要包含的標頭檔: <fstream> 

名字空間: std

也可以試用<fstream.h>

fstream提供了三個類,用來實現c++對檔案的操作。(檔案的建立,讀寫)。
ifstream -- 從已有的檔案讀

ofstream -- 向檔案寫內容

fstream - 開啟檔案供讀寫

支援的檔案類型

實際上,檔案類型可以分為兩種: 文字檔和二進位檔案.

文字檔儲存的是可讀的字元, 而二進位檔案儲存的只是位元據。利用二進位模式,你可以操作映像等檔案。用文字模式,你只能讀寫文字檔。否則會報錯。

 

例一: 寫檔案

聲明一個ostream變數

  1. 調用open方法,使其與一個檔案關聯
  2. 寫檔案
  3. 調用close方法.
  1. #include <fstream.h>
  2.  
  3. void main
  4. {
  5. ofstream file;
  6.  
  7. file.open("file.txt");
  8.  
  9. file<<"Hello file/n"<<75;
  10.  
  11. file.close();
  12. }

可以像試用cout一樣試用操作符<<向檔案寫內容.
Usages:

  1.  
  2. file<<"string/n";
  3. file.put('c');

例二:  讀檔案

1. 聲明一個ifstream變數.

2. 開啟檔案.

3. 從檔案讀資料

4. 關閉檔案.

  1. #include <fstream.h>
  2.  
  3. void main
  4. {
  5. ifstream file;
  6. char output[100];
  7. int x;
  8.  
  9. file.open("file.txt");
  10.  
  11. file>>output;
  12. cout<<output;
  13. file>>x;
  14. cout<<x;
  15.  
  16. file.close();
  17. } 同樣的,你也可以像cin一樣使用>>來操作檔案。或者是調用成員函數Usages:
  1.  
  2. file>>char *;
  3. file>>char;
  4. file.get(char);
  5. file.get(char *,int);
  6. file.getline(char *,int sz);
  7. file.getline(char *,int sz,char eol);

1.同樣的,你也可以使用建構函式開開啟一個檔案、你只要把檔案名稱作為建構函式的

第一個參數就可以了。

  1. ofstream file("fl.txt");
  2. ifstream file("fl.txt");

上面所講的ofstream和ifstream只能進行讀或是寫,而fstream則同時提供讀寫的功能。
void main()

  1. {
  2. fstream file;
  3.  
  4. file.open("file.ext",iso::in|ios::out)
  5.  
  6. //do an input or output here
  7.  
  8. file.close();
  9. }

open函數的參數定義了檔案的開啟模式。總共有如下模式

  1. 屬性列表
  2.  
  3. ios::in 讀
  4. ios::out寫
  5. ios::app從檔案末尾開始寫
  6. ios::binary 二進位模式
  7. ios::nocreate開啟一個檔案時,如果檔案不存在,不建立檔案。
  8. ios::noreplace開啟一個檔案時,如果檔案不存在,建立該檔案
  9. ios::trunc開啟一個檔案,然後清空內容
  10. ios::ate開啟一個檔案時,將位置移動到檔案尾

Notes

  • 預設模式是文本
  • 預設如果檔案不存在,那麼建立一個新的
  • 多種模式可以混合,用|(按位或)
  • 檔案的byte索引從0開始。(就像數組一樣)

我們也可以調用read函數和write函數來讀寫檔案。

 

檔案指標位置在c++中的用法:

  1. ios::beg檔案頭
  2. ios::end檔案尾
  3. ios::cur當前位置例子:
  1. file.seekg(0,ios::end);
  2.  
  3. int fl_sz = file.tellg();
  4.  
  5. file.seekg(0,ios::beg);

常用的錯誤判斷方法:

  1. good()如果檔案開啟成功
  2. bad()開啟檔案時發生錯誤
  3. eof()到達檔案尾例子:
  1. char ch;
  2. ifstream file("kool.cpp",ios::in|ios::out);
  3.  
  4. if(file.good()) cout<<"The file has been opened without problems;
  5. else cout<<"An Error has happend on opening the file;
  6.  
  7. while(!file.eof())
  8. {
  9. file>>ch;
  10. cout<<ch;
  11. }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.