C++ ofstream 類
示例
使用 ofstream
向檔案寫入資料
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Create and open a text file
ofstream MyFile("filename.txt");
// Write to the file
MyFile << "Files can be tricky, but it is fun enough!";
// Close the file
MyFile.close();
}
定義和用法
ofstream
類(“output file stream” 的縮寫)用於向檔案中寫入內容。
ofstream
類定義在 <fstream>
標頭檔案中。
要開啟一個檔案,將檔案路徑傳遞給建構函式
ofstream MyFile("filename.txt");
可以使用 <<
插入運算子和各種函式來寫入檔案。
插入運算子
<<
插入運算子將字面值或變數內容寫入檔案。
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; |
檔案寫入函式
檔案寫入函式將資料寫入檔案,並將檔案指標移動到寫入內容之後的第一個位置。
write()
write(str, n)
方法將 char
陣列 str
中的 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。
ofstream 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();
seekp()
seekp(position)
方法將檔案指標移動到相對於檔案開頭的指定位置。
MyFile.seekp(6)
seekp(position, origin)
方法將檔案指標移動到檔案中的指定 position,該位置相對於 origin。origin 有三個可能的值:
ofstream::beg
- 位置相對於檔案開頭。ofstream::cur
- 位置相對於當前檔案位置。ofstream::end
- 位置相對於檔案末尾。
將檔案指標移動到不同位置
MyFile.seekp(6, ofstream::beg);
cout << MyFile.tellp(); << "\n";
MyFile.seekp(-3, ofstream::cur);
cout << MyFile.tellp(); << "\n";
MyFile.seekp(-4, ofstream::end);
cout << MyFile.tellp(); << "\n";
tellp()
tellp()
方法返回檔案中檔案指標的當前位置。
cout << MyFile.tellp();