C++ ofstream Class
示例
使用 ofstream 写入文件:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// 创建并打开文本文件
ofstream MyFile("filename.txt");
// 写入文件
MyFile << "Files can be tricky, but it is fun enough!";
// 关闭文件
MyFile.close();
}
定义和用法
ofstream 类(即"输出文件流")用于将数据写入文件。
ofstream 类定义在 <fstream> 头文件中。
要打开文件,请将文件路径传递给构造函数:
ofstream MyFile("filename.txt");
<<可以使用插入运算符和各种函数向文件中写入内容。
插入运算符
<< 插入运算符将字面值或变量的内容写入文件。
int year = 2024;
MyFile << year << "
";
MyFile << "Files can be tricky, but it is fun enough!";
操纵符
操纵符用于更改写入文件的数据格式。它们与字面值和变量一样,与插入运算符 << 一起使用。
除了 setw() 之外,操纵符的效果会一直持续,直到被另一个操纵符更改为止。
下表列出了一些常用的操纵符。
| 操纵符 | 描述 | 示例 |
|---|---|---|
boolalpha |
将布尔值写入"true"和"false",而不是"1"和"0"。 | MyFile << boolalpha << false; |
dec |
将整数表示为十进制数字。 | MyFile << dec << 12; |
endl |
写入换行符。此操作符还会清空输出缓冲区,因此其效率低于打印
。 |
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);
File Handling Functions
File handling functions open, close and navigate files.
open()
open(filepath) 方法打开由 filepath 指定的路径下的文件。如果文件已打开,则此方法无效。
ofstream MyFile;
MyFile.open("filename.txt");
is_open()
is_open() 方法在文件打开时返回 true,在没有文件打开时返回 false。
ofstream MyFile;
cout << MyFile.is_open(); << "
"; // 显示 0,因为文件未打开。
MyFile.open("filename.txt");
cout << MyFile.is_open(); << "
"; // 显示 1,因为文件已打开
close()
close() 方法用于关闭文件。使用完毕后,最好关闭文件以释放资源。
MyFile.close();
rdbuf()
rdbuf() 方法返回指向内部 filebuf 对象的指针,该对象直接处理文件。
filebuf * buf = MyFile.rdbuf();
seekp()
seekp(position) 方法将文件指针移动到相对于文件开头的指定位置。
MyFile.seekp(6)
seekp(position, origin) 方法将文件指针移动到相对于 origin 的指定 position 位置。
ofstream::beg- 位置相对于文件开头。ofstream::cur- 位置相对于当前文件位置。ofstream::end- 位置相对于文件末尾。
Move the file pointer to different positions:
MyFile.seekp(6, ofstream::beg);
cout << MyFile.tellp(); << "
";
MyFile.seekp(-3, ofstream::cur);
cout << MyFile.tellp(); << "
";
MyFile.seekp(-4, ofstream::end);
cout << MyFile.tellp(); << "
";
tellp()
tellp() 方法返回文件指针在文件中的当前位置。
cout << MyFile.tellp();

