gpt4 book ai didi

文件的 C++ 输出流不起作用

转载 作者:行者123 更新时间:2023-11-28 01:41:09 26 4
gpt4 key购买 nike

我的代码如下。缓冲区有数据,但 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);

关于文件的 C++ 输出流不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47108586/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com