试图在c++中操作文件
Trying to manipulate files in c++
当使用<fstream>
库打开并向现有文件添加流时,test.rtf
和我使用以下行:
char data[100];
// open a file in write mode.
ofstream outfile;
outfile.open("test.rtf");
if (outfile.is_open()) { cout << "file is open" << endl; }
cout << "Writing to the file" << endl;
cout << "Enter your name: ";
cin.getline(data, 100);
// write inputted data into the file.
outfile << data << endl;
当使用ifstream读取时,输入的行会正确显示。问题是输出文件没有被修改,我添加的行也没有保存。这个问题听起来可能很愚蠢,但这是一个我无法解决的问题。
当您<lt;对于您的文件,您只是在向缓冲区写入,而不是将其"刷新"到文件本身。如果你只是关闭你的文件,你应该没事。
因此:
outfile.close()
此外,在将来,当您想写入文件但不关闭它时,您可以刷新(实际上是从缓冲区写入文件)。close()会自动为您刷新然后关闭。
相关文章:
- C++二进制文件 I/O 操作速度变慢.数据库如何处理二进制文件?
- RPN计算器使用头文件进行计算操作和堆栈;用于堆栈的矢量
- 我是否缺少<实验/文件系统>移动操作?
- C++ 使用读取目录更改监视文件更改W不触发所有操作?
- 在链接的程序集文件中,我想从 c++ 调用代码访问变量.是否可以在不触发访问冲突的情况下执行此操作?
- 程序未正确执行文件操作
- 尝试在 QT 中控制文件-QMenu 操作
- 打开一个文件,修改每个字符,然后执行反向操作不输出原始文件
- 如何在我的进程中运行的二进制文件中注册操作和内核
- exe文件不被识别为内部或外部命令,可操作的程序或批处理文件在C++
- C++程序在执行文件操作时在Windows与Linux上运行得慢得多?
- 如何处理对生成文件中依赖项的重命名操作?
- 使用文件操作的 XMLFile 解析
- 从文件中读取运算符并执行操作C++
- 为什么我的文件/目录操作在Windows上工作正常,但在Linux上不能
- 使用函数操作文件
- 试图在c++中操作文件
- 除了 Boost 的文件系统之外,是否有一个好的C++库来操作文件和目录?
- C++:操作文件资源
- Qt Creator在使用其C++编辑器操作文件时崩溃