C++ fstream 類
示例
使用 fstream
讀寫檔案
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Create and open a text file
fstream MyFile("filename.txt");
// Write to the file
MyFile << "Files can be tricky, but it is fun enough!";
// Read from the file
string myText;
getline(MyFile, myText);
cout << myText;
// Close the file
MyFile.close();
}
定義和用法
fstream
類("file stream" 的縮寫)用於讀寫檔案。
fstream
類定義在 <fstream>
標頭檔案中。
要開啟一個檔案,將檔案路徑傳遞給建構函式
fstream MyFile("filename.txt");
fstream
類有多種用於讀寫檔案的函式,如下所列。
檔案指標函式
檔案指標是內部變數,指示在檔案中的何處進行讀寫。
檔案指標函式用於操作檔案指標。有用於讀檔案指標和寫檔案指標的函式,但 fstream
類對這兩個操作使用相同的指標,因此改變其中一個也會改變另一個。
seekg()
seekg(position)
方法將讀檔案指標移動到相對於檔案開頭的指定位置。
MyFile.seekg(6)
seekg(position, origin)
方法將讀檔案指標移動到相對於某個源點的指定位置。源點有三個可能的值:
fstream::beg
- 位置相對於檔案開頭。fstream::cur
- 位置相對於當前檔案位置。fstream::end
- 位置相對於檔案末尾。
將讀檔案指標移動到不同位置
MyFile.seekg(6, fstream::beg);
cout << MyFile.tellg() << "\n";
MyFile.seekg(-3, fstream::cur);
cout << MyFile.tellg() << "\n";
MyFile.seekg(-4, fstream::end);
cout << MyFile.tellg() << "\n";
tellg()
tellg()
方法返回檔案指標在檔案中的當前位置。
cout << MyFile.tellg();
seekp()
seekp(position)
方法將寫檔案指標移動到相對於檔案開頭的指定位置。
MyFile.seekp(6)
seekp(position, origin)
方法將寫檔案指標移動到相對於某個源點的指定位置。源點有三個可能的值:
fstream::beg
- 位置相對於檔案開頭。fstream::cur
- 位置相對於當前檔案位置。fstream::end
- 位置相對於檔案末尾。
將寫檔案指標移動到不同位置
MyFile.seekp(6, fstream::beg);
cout << MyFile.tellp() << "\n";
MyFile.seekp(-3, fstream::cur);
cout << MyFile.tellp() << "\n";
MyFile.seekp(-4, fstream::end);
cout << MyFile.tellp() << "\n";
tellp()
tellp()
方法返回寫檔案指標在檔案中的當前位置。
cout << MyFile.tellp();
檔案讀取函式
檔案讀取函式從檔案中提取字元並移動檔案指標。
get()
get()
方法從檔案中讀取單個字元,並以 int
值的形式返回其 ASCII 值。將其轉換為 char
型別以檢視該字元。檔案指標移動到檔案中的下一個字元。
char myChar = MyFile.get();
cout << myChar;
get(destination, size, delimiter)
方法將從檔案中讀取的資料最多 size 個字元寫入目標位置。一旦遇到換行符、檔案末尾或由 delimiter 引數給定的可選字元,它就會停止讀取。寫入 destination 的值總是以 \0
空終止符結尾。此方法將檔案指標移動到它停止讀取的換行符或分隔符處。
char destination[20];
MyFile.get(destination, 20);
cout << destination << "\n";
// Stop reading when a '.' is found
MyFile.get(destination, 20, '.');
cout << destination << "\n";
getline()
getline(destination, size, delimiter)
方法與 get(destination, size, delimiter)
方法相同,只是換行符或分隔符被丟棄,檔案指標移動到其後的字元。
char destination[20];
MyFile.getline(destination, 20);
cout << destination << "\n";
// Stop reading when a '.' is found
MyFile.getline(destination, 20, '.');
cout << destination << "\n";
還有一個類似的 getline(stream, destination, delimiter)
函式,它從 stream 引數中的 fstream
物件指定的檔案中讀取所有字元,直到下一個換行符(或可選的 delimiter),並將它們寫入 destination 指定的字串中。
string destination;
getline(MyFile, destination);
cout << destination << "\n";
// Stop reading when a '.' is found
getline(MyFile, destination, '.');
cout << destination << "\n";
read()
read(destination, n)
方法從檔案中讀取 n 個字元,並將它們寫入 destination 引數指定的 char
陣列中。與其他函式不同,它不會在換行符處停止讀取,也不會在資料末尾新增空終止符。
char destination[20];
MyFile.read(destination, 19);
destination[20] = '\0'; // Make sure it ends with a null terminating character
cout << destination << "\n";
peek()
peek()
方法從檔案中讀取單個字元,並以 int
值的形式返回其 ASCII 值。將其轉換為 char
型別以檢視該字元。與 get()
方法不同,此方法不移動檔案指標。
char myChar = MyFile.peek();
cout << myChar;
gcount()
gcount()
方法返回最近呼叫的檔案讀取方法從檔案中提取的字元數。
char destination[20];
MyFile.getline(destination, 20);
cout << MyFile.gcount() << "\n";
檔案寫入函式
檔案寫入函式將資料寫入檔案,並將檔案指標移動到寫入內容後的第一個位置。
write()
write(str, n)
方法將 char
陣列 str 中的 n 個字元寫入檔案,並將檔案指標向前移動 n 個字元。
char myStr[] = "Hello World!";
MyFile.write(myStr, 5);
put()
put(c)
方法將指定的字元 c 寫入檔案,並將檔案指標向前移動一個字元。
char grade = 'B';
MyFile.put(grade);
檔案處理函式
檔案處理函式開啟和關閉檔案。
open()
open(filepath)
方法開啟 filepath 指定路徑的檔案。如果檔案已經開啟,則此方法無效。
ofstream MyFile;
MyFile.open("filename.txt");
is_open()
is_open()
方法如果檔案已開啟則返回 true,如果沒有檔案開啟則返回 false。
fstream MyFile;
cout << MyFile.is_open(); << "\n"; // Displays 0 because the file is not open
MyFile.open("filename.txt");
cout << MyFile.is_open(); << "\n"; // Displays 1 because the file is open
close()
close()
方法關閉檔案。完成檔案操作後關閉檔案以釋放資源是一個好習慣。
MyFile.close();
rdbuf()
rdbuf()
方法返回一個指向內部 filebuf
物件的指標,該物件直接處理檔案。
filebuf * buf = MyFile.rdbuf();
提取運算子
>>
提取運算子從檔案中的當前位置讀取多個字元,對其進行解釋,並將解釋後的值寫入變數中。然後檔案指標移動到下一個尚未讀取的字元。字元的解釋方式取決於變數的資料型別。
語法
MyFile >> variable
它也可以多次使用,以一個接一個地讀取檔案的各個部分。
MyFile >> variable1 >> variable2 >> variable3
>>
提取運算子首先跳過空白字元(空格、製表符和換行符),直到遇到第一個非空白字元。之後,它根據變數的資料型別遵循下表中的規則。
資料型別 | 描述 | 示例 |
---|---|---|
int long short
|
讀取一系列數字並將其解釋為整數。該序列前面可以有一個符號(“+”或“-”)。它在遇到第一個非數字字元時停止讀取。 如果未找到有效序列, ifstream 物件將失敗並停止進一步讀取。 |
15 |
bool |
按上述方式讀取一個整數,然後將 0 解釋為 false,將 1 解釋為 true。任何其他整數值都將被解釋為 true,但 ifstream 物件將失敗並停止進一步讀取。下一節中描述的 boolalpha 運算子完全改變了這種行為。 |
0 |
float double
|
讀取有效的字元序列並將其解釋為浮點數。有效序列至少有一個數字,前面可以有一個符號(“+”或“-”),後面可以跟一個小數點和十進位制數字。也可以使用科學記數法(一個數字後跟“e”或“E”和一些數字)。 如果未找到有效序列, fstream 物件將失敗並停止進一步讀取。 |
5 |
char
|
從檔案中讀取單個字元。 如果檔案指標在檔案末尾, fstream 物件將失敗並停止進一步讀取。 |
B |
string char *
|
讀取所有字元,直到下一個空白(空格、製表符或換行符)、空終止符或檔案末尾。變數的值將新增一個 \0 空終止符。如果檔案指標已經在一個空終止符或檔案末尾, fstream 物件將失敗並停止進一步讀取。 |
Hello |
運算子
運算子可以代替變數使用。使用運算子時,它們會改變 fstream
物件解釋資料的方式。運算子的效果會一直保持,直到另一個運算子改變它。
下表列出了可以與 >>
提取運算子一起使用的運算子。
運算子 | 描述 |
---|---|
noskipws |
>> 提取運算子將讀取空白字元,而不是跳過它們。這主要對 char 型別變數有用,因為對於其他資料型別,它在遇到空白時會停止讀取。 |
skipws |
重置由 noskipws 運算子所做的更改。 |
ws |
將檔案指標移動到檔案中下一個沒有空白的位置。 |
hex |
在使用整數變數時,期望數字的十六進位制表示(0 到 9 和 A 到 F)。 |
oct |
在使用整數變數時,期望數字的八進位制表示(0 到 7)。 |
dec |
在使用整數變數時,期望數字的十進位制表示(0 到 9)。這將重置由 hex 和 oct 運算子所做的更改。 |
boolalpha |
為布林變數讀取資料時,它會查詢字元序列 "true" 或 "false",而不是查詢整數。 |
noboolalpha |
重置由 boolalpha 運算子所做的更改。 |
示例
使用運算子改變資料的解釋方式
bool myBool;
int myInt;
// Interpret character sequences "true" and "false" as boolean values
MyFile >> boolalpha >> myBool;
// Revert to reading booleans normally
MyFile >> noboolalpha;
// Read hexadecimal numbers from the file and interpret them as integers
MyFile >> hex >> myInt;
// Revert to reading integers normally
MyFile >> dec;
插入運算子
<<
插入運算子將字面值或變數的內容寫入檔案。
int year = 2024;
MyFile << year << "\n";
MyFile << "Files can be tricky, but it is fun enough!";
運算子
運算子改變寫入檔案的資料的格式。它們與 <<
插入運算子一起使用,方式與字面值和變數相同。
除了 setw()
,運算子的效果會一直保持,直到另一個運算子改變它。
下表顯示了有用的運算子列表
運算子 | 描述 | 示例 |
---|---|---|
boolalpha |
將布林值寫為 "true" 和 "false",而不是 "1" 和 "0"。 | MyFile << boolalpha << false; |
dec |
將整數表示為十進位制數字。 | MyFile << dec << 12; |
endl |
寫入一個換行符。此運算子還會重新整理輸出緩衝區,這使得它比列印 \n 效率低。 |
MyFile << "Line 1" << endl << "Line 2"; |
ends |
寫入用於結束 C 風格字串的 \0 空終止符。 |
MyFile << "Hello World!" << ends; |
fixed |
用固定的小數位數表示浮點數。小數位數可以使用 setprecision() 運算子來確定。 |
MyFile << fixed << 19.99; |
hex |
將整數表示為十六進位制數字。 | MyFile << hex << 12; |
internal |
如果指定了寬度(使用 setw() 運算子),數字的符號將左對齊,而值將右對齊,其他資料型別的輸出將右對齊。 |
MyFile << setw(10) << internal << -12345; |
left |
如果指定了寬度(使用 setw() 運算子),則將輸出左對齊。 |
MyFile << setw(10) << left << "Hello"; |
noboolalpha |
用於重置由 boolalpha 運算子所做的更改。 |
MyFile << noboolalpha << false; |
noshowbase |
用於重置由 showbase 運算子所做的更改。 |
MyFile << hex << noshowbase << 12; |
noshowpoint |
用於重置由 showpoint 運算子所做的更改。 |
MyFile << noshowpoint << 12345.0; |
noshowpos |
用於重置由 showpos 運算子所做的更改。 |
MyFile << noshowpos << 12; |
nouppercase |
用於重置由 uppercase 運算子所做的更改。 |
MyFile << hex << nouppercase << 12; |
oct |
將整數表示為八進位制數字。 | MyFile << oct << 12; |
right |
如果指定了寬度(使用 setw() 運算子),則將輸出右對齊。 |
MyFile << setw(10) << right << "Hello"; |
scientific |
用科學記數法表示浮點數。小數位數可以使用 setprecision() 運算子來確定。 |
MyFile << fixed << 19.99; |
setfill() |
選擇一個字元用作填充。 需要 <iomanip> 庫。 |
MyFile << setfill('.') << setw(10) << 19.99; |
setprecision() |
選擇浮點數的精度。如果使用了 fixed 或 scientific 運算子,它指定小數位數,否則它指定有效數字的數量。需要 <iomanip> 庫。 |
MyFile << setprecision(4) << 12.3456; |
setw() |
指定下一個輸出的最小字元寬度。如果輸出不夠寬,則新增填充以填充剩餘空間。 需要 <iomanip> 庫。 |
MyFile << setw(10) << "Hello"; |
showbase |
將整數表示為十六進位制或八進位制時,在數字前加上 "0x" 或 "0" 以顯示其基數。 | MyFile << hex << showbase << 12; |
showpoint |
總是為浮點數寫入小數點,即使不需要。 | MyFile << showpoint << 12345.0; |
showpos |
總是在正數旁邊寫入 + 號。 | MyFile << showpos << 12; |
uppercase |
用大寫字母表示十六進位制數字和科學記數法中的 "e"。 | MyFile << hex << uppercase << 12; |