C++ 檔案和流



到目前為止,我們一直在使用iostream標準庫,它提供cincout方法分別用於從標準輸入讀取和寫入標準輸出。

本教程將教你如何從檔案讀取和寫入檔案。這需要另一個名為fstream的標準C++庫,它定義了三種新的資料型別:

序號 資料型別和描述
1

ofstream

此資料型別表示輸出檔案流,用於建立檔案並將資訊寫入檔案。

2

ifstream

此資料型別表示輸入檔案流,用於從檔案讀取資訊。

3

fstream

此資料型別通常表示檔案流,並且具有ofstream和ifstream的功能,這意味著它可以建立檔案,將資訊寫入檔案以及從檔案讀取資訊。

要在C++中執行檔案處理,必須在C++原始檔中包含標頭檔案<iostream>和<fstream>。

開啟檔案

在讀取或寫入檔案之前必須先開啟檔案。可以使用ofstreamfstream物件開啟要寫入的檔案。而ifstream物件僅用於開啟要讀取的檔案。

以下是open()函式的標準語法,它是fstream、ifstream和ofstream物件的成員。

void open(const char *filename, ios::openmode mode);

這裡,第一個引數指定要開啟的檔案的名稱和位置,open()成員函式的第二個引數定義應開啟檔案的模式。

序號 模式標誌和描述
1

ios::app

追加模式。所有寫入該檔案的內容都將追加到末尾。

2

ios::ate

開啟一個輸出檔案並將讀/寫控制移動到檔案的末尾。

3

ios::in

開啟一個用於讀取的檔案。

4

ios::out

開啟一個用於寫入的檔案。

5

ios::trunc

如果檔案已存在,則在開啟檔案之前將截斷其內容。

您可以透過將它們一起OR來組合兩個或多個這些值。例如,如果您想以寫入模式開啟檔案,並且如果檔案已存在則要截斷它,則語法如下:

ofstream outfile;
outfile.open("file.dat", ios::out | ios::trunc );

類似地,您可以按如下方式開啟一個用於讀寫目的的檔案:

fstream  afile;
afile.open("file.dat", ios::out | ios::in );

關閉檔案

當C++程式終止時,它會自動重新整理所有流,釋放所有已分配的記憶體並關閉所有開啟的檔案。但是,程式設計師在程式終止前關閉所有開啟的檔案始終是一個好習慣。

以下是close()函式的標準語法,它是fstream、ifstream和ofstream物件的成員。

void close();

寫入檔案

在進行C++程式設計時,您可以使用流插入運算子(<<)將資訊從程式寫入檔案,就像使用該運算子將資訊輸出到螢幕一樣。唯一的區別是您使用ofstreamfstream物件而不是cout物件。

從檔案讀取

您可以使用流提取運算子(>>)將資訊從檔案讀取到程式中,就像使用該運算子從鍵盤輸入資訊一樣。唯一的區別是您使用ifstreamfstream物件而不是cin物件。

讀寫示例

以下是C++程式,它以讀寫模式開啟檔案。在將使用者輸入的資訊寫入名為afile.dat的檔案後,程式從檔案中讀取資訊並將其輸出到螢幕上:

#include <fstream>
#include <iostream>
using namespace std;
 
int main () {
   char data[100];

   // open a file in write mode.
   ofstream outfile;
   outfile.open("afile.dat");

   cout << "Writing to the file" << endl;
   cout << "Enter your name: "; 
   cin.getline(data, 100);

   // write inputted data into the file.
   outfile << data << endl;

   cout << "Enter your age: "; 
   cin >> data;
   cin.ignore();
   
   // again write inputted data into the file.
   outfile << data << endl;

   // close the opened file.
   outfile.close();

   // open a file in read mode.
   ifstream infile; 
   infile.open("afile.dat"); 
 
   cout << "Reading from the file" << endl; 
   infile >> data; 

   // write the data at the screen.
   cout << data << endl;
   
   // again read the data from the file and display it.
   infile >> data; 
   cout << data << endl; 

   // close the opened file.
   infile.close();

   return 0;
}

編譯並執行上述程式碼時,它會產生以下示例輸入和輸出:

$./a.out
Writing to the file
Enter your name: Zara
Enter your age: 9
Reading from the file
Zara
9

上面的示例使用了cin物件的附加函式,例如getline()函式從外部讀取行,以及ignore()函式忽略先前讀取語句留下的額外字元。

檔案位置指標

istreamostream都提供用於重新定位檔案位置指標的成員函式。這些成員函式對於istream是seekg(“seek get”),對於ostream是seekp(“seek put”)。

seekg和seekp的引數通常是長整數。可以指定第二個引數來指示搜尋方向。搜尋方向可以是ios::beg(預設值),用於相對於流的開頭定位;ios::cur,用於相對於流中的當前位置定位;或者ios::end,用於相對於流的末尾定位。

檔案位置指標是一個整數值,它指定檔案中距檔案起始位置的位元組數。

// position to the nth byte of fileObject (assumes ios::beg)
fileObject.seekg( n );

// position n bytes forward in fileObject
fileObject.seekg( n, ios::cur );

// position n bytes back from end of fileObject
fileObject.seekg( n, ios::end );

// position at end of fileObject
fileObject.seekg( 0, ios::end );
廣告