C++ ifstream 類
示例
使用 ifstream
從檔案中讀取行
// Create a text string, which is used to output the text file
string myText;
// Read from the text file
ifstream MyReadFile("filename.txt");
// Use a while loop together with the getline() function to read the file line by line
while (getline (MyReadFile, myText)) {
// Output the text from the file
cout << myText;
}
// Close the file
MyReadFile.close();
定義和用法
ifstream
類("輸入檔案流"的縮寫)用於從檔案中讀取資料。
ifstream
類定義在 <fstream>
標頭檔案中。
要開啟一個檔案,將檔案路徑傳遞給建構函式
ifstream MyReadFile("filename.txt");
ifstream
類有許多從檔案中讀取資料的方法。一個簡單的方法是使用 getline()
函式讀取所有字元直到下一個換行符,並將它們寫入字串。
輸出檔案中的一行文字
string myText;
getline(MyReadFile, myText);
cout << myText;
檔案讀取函式
檔案讀取函式從檔案中提取字元並移動檔案指標。
get()
get()
方法從檔案中讀取單個字元,並將其 ASCII 值作為 int
值返回。將其轉換為 char
型別即可看到字元。檔案指標會移動到檔案中的下一個字元。
char myChar = MyReadFile.get();
cout << myChar;
get(destination, size, delimiter)
方法將最多 size 個字元從檔案中讀取並寫入 destination。它會在遇到換行符、檔案結尾或 delimiter 引數指定的可選字元時停止讀取。寫入 destination 的值總是以 \0
空終止符結尾。此方法會將檔案指標移動到它停止讀取的換行符或分隔符處。
char destination[20];
MyReadFile.get(destination, 20);
cout << destination << "\n";
// Stop reading when a '.' is found
MyReadFile.get(destination, 20, '.');
cout << destination << "\n";
getline()
getline(destination, size, delimiter)
方法與 get(destination, size, delimiter)
方法相同,只是它會丟棄換行符或分隔符,並將檔案指標移動到其後面的字元。
char destination[20];
MyReadFile.getline(destination, 20);
cout << destination << "\n";
// Stop reading when a '.' is found
MyReadFile.getline(destination, 20, '.');
cout << destination << "\n";
有一個類似的 getline(stream, destination, delimiter)
**函式**,它從 stream 引數中指定的 ifstream
物件指向的檔案中讀取所有字元直到下一個換行符(或可選的 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];
MyReadFile.read(destination, 19);
destination[20] = '\0'; // Make sure it ends with a null terminating character
cout << destination << "\n";
peek()
peek()
方法從檔案中讀取單個字元,並將其 ASCII 值作為 int
值返回。將其轉換為 char
型別即可看到字元。與 get()
方法不同,此方法不會移動檔案指標。
char myChar = MyReadFile.peek();
cout << myChar;
gcount()
gcount()
方法返回最近一次呼叫的檔案讀取方法從檔案中提取的字元數。
char destination[20];
MyReadFile.getline(destination, 20);
cout << MyReadFile.gcount() << "\n";
檔案處理函式
檔案處理函式用於開啟、關閉和導航檔案。
open()
open(filepath)
方法開啟 filepath 指定路徑的檔案。如果檔案已經開啟,則此方法無效。
ifstream MyReadFile;
MyReadFile.open("filename.txt");
is_open()
is_open()
方法如果檔案已開啟則返回 true,如果沒有檔案開啟則返回 false。
ifstream MyReadFile;
cout << MyReadFile.is_open(); << "\n"; // Displays 0 because the file is not open
MyReadFile.open("filename.txt");
cout << MyReadFile.is_open(); << "\n"; // Displays 1 because the file is open
close()
close()
方法關閉檔案。完成檔案操作後關閉檔案以釋放資源是一個好習慣。
MyReadFile.close();
rdbuf()
rdbuf()
方法返回一個指向內部 filebuf
物件的指標,該物件直接處理檔案。
filebuf * buf = MyReadFile.rdbuf();
unget()
unget()
方法將檔案指標向後移動一個字元。
使用 unget()
方法列印同一個字元兩次
char myChar = MyReadFile.get();
cout << myChar << "\n";
MyReadFile.unget();
myChar = MyReadFile.get();
cout << myChar;
seekg()
seekg(position)
方法將檔案指標移動到檔案開頭指定的位置。
MyReadFile.seekg(6)
seekg(position, origin)
方法將檔案指標移動到檔案中相對於 origin 的指定 position。origin 有三個可能的值:
ifstream::beg
- 位置相對於檔案開頭。ifstream::cur
- 位置相對於當前檔案位置。ifstream::end
- 位置相對於檔案結尾。
移動檔案指標到不同位置
MyReadFile.seekg(6, ifstream::beg);
cout << MyReadFile.tellg(); << "\n";
MyReadFile.seekg(-3, ifstream::cur);
cout << MyReadFile.tellg(); << "\n";
MyReadFile.seekg(-4, ifstream::end);
cout << MyReadFile.tellg(); << "\n";
tellg()
tellg()
方法返回檔案中檔案指標的當前位置。
cout << MyReadFile.tellg();
提取運算子
>>
提取運算子從檔案當前位置讀取一定數量的字元,解釋它們並將解釋後的值寫入變數。然後檔案指標移動到下一個尚未讀取的字元。字元的解釋方式取決於變數的資料型別。
語法
MyReadFile >> variable
它也可以多次使用,以順序讀取檔案的不同部分。
MyReadFile >> variable1 >> variable2 >> variable3
>>
提取運算子首先跳過空白字元(空格、製表符和換行符),直到遇到第一個非空白字元。之後,它將根據變數的資料型別遵循下表所示的規則。
資料型別 | 描述 | 示例 |
---|---|---|
int long short
|
讀取一串數字並將其解釋為整數。該串數字前面可以有一個符號("+" 或 "-")。它會在遇到第一個非數字字元時停止讀取。 如果沒有找到有效序列, ifstream 物件將失敗並停止進一步讀取。 |
15 |
bool |
讀取一個整數(方式與上面描述的相同),然後將 0 解釋為 *false*,將 1 解釋為 *true*。任何其他整數值都將被解釋為 *true*,但 ifstream 物件將失敗並停止進一步讀取。下一節中描述的 boolalpha 操縱符會完全改變此行為。 |
0 |
float double
|
讀取有效的字元序列並將其解釋為浮點數。有效序列至少有一個數字,前面可以有一個符號(“+”或“-”),後面可以跟一個小數點和十進位制數字。也可以使用科學記數法(一個數字後跟“e”或“E”和一些數字)。 如果沒有找到有效序列, ifstream 物件將失敗並停止進一步讀取。 |
5 |
char
|
從檔案中讀取單個字元。 如果檔案指標已到達檔案末尾, ifstream 物件將失敗並停止進一步讀取。 |
B |
string char *
|
讀取直到下一個空白字元(空格、製表符或換行符)、空終止符或檔案末尾的所有字元。變數的值將新增一個 \0 空終止符。如果檔案指標已處於空終止符或檔案末尾, ifstream 物件將失敗並停止進一步讀取。 |
Hello |
運算子
可以使用操縱符代替變數。使用操縱符時,它們會改變 ifstream
物件解釋資料的方式。操縱符的效果會一直保持,直到被另一個操縱符更改。
下表列出了 ifstream
物件可以使用的一系列操縱符。
運算子 | 描述 |
---|---|
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;