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; |