C++输出流到文件不起作用
C++ output stream to a file is not working
我的代码在下面。缓冲区有数据,但 fout2.write 不做任何事情。文件已创建,并且为空。
ofstream fout2(fname, ios::binary);
fout2.open(fname, ios::binary | ios::in | ios::out);
if (fout2.is_open()) {
//problem is here //write the buffer contents
fout2.write(rmsg.buffer, rmsg.length);
fout2.flush();
memset(rmsg.buffer, 0, sizeof(rmsg.buffer)); //clear the buffer
如果你打算同时做输入和输出,正如你使用 ios::in
所暗示的那样,你应该使用 fstream
,而不是ofstream
。然后你应该在构造函数中传递所有开放模式,并且不需要调用open()
。
fstream fout2(fname, ios::binary | ios::in | ios::out);
您可能忘记关闭文件。你可以通过以下方式做
fout2.close()
或者通过简单地关闭 fout2 的范围:
{
ofstream fout2(fname, ios::binary);
fout2.open(fname, ios::binary | ios::in | ios::out);
if (fout2.is_open()) {
fout2.write(rmsg.buffer, rmsg.length);
//out2.flush(); // no need for this
memset(rmsg.buffer, 0, sizeof(rmsg.buffer)); //clear the buffer
}
}
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在其他文件中创建类时在 c++ 项目中不起作用
- g++ -I 和 -L 命令来查找不起作用的 .hpp 文件
- 该程序将.csv文件中的一系列单词放入数组中,然后随机生成句子.但它不起作用
- 无法打开包含文件'Graphics.hpp'没有这样的文件或目录,Visual Studio的其他包含不起作用
- 从类中的另一个文件请求函数不起作用
- 即使我正在.cpp文件中实例化一个伪对象,.cpp文件内模板函数的定义也不起作用
- 简单的二分搜索对我不起作用。我正在尝试从文本文件中搜索单词
- SFML 加载从文件不起作用,奇怪的错误
- C++密钥和文件的映射不起作用
- 从 TXT 文件中删除行不起作用
- QT QML:使用.qrc文件将SQLite数据库部署到Android不起作用
- bcdedit 文件输出不起作用,但其他命令可以
- 文件写入在PHP扩展c / c ++(linux)中不起作用
- Makefile在Linux中工作,但在Windows下不起作用,在子目录中找不到文件
- 使用 libcurl C++将文件下载到 Ubuntu,简单示例不起作用
- 尝试读取数据文件,存储在数组中并打印所有元素,但它不起作用
- 为什么写入文件不起作用
- 包括不起作用C 的文件
- 在 C++ 中,.h 文件包含文件但不起作用