c++ - 打开二进制文件写入 : terminate called after throwing an instance of 'std::ios_base::failure' 时出错

标签 c++

我从以下代码块得到以下输出:

    //create file for writing
cout << "'" << filename.c_str() << "'" << endl;
    string outfile = filename.append(".bin");
cout << "'" << outfile.c_str() << "'" << endl;
    fstream *binfile;
    binfile->open (outfile.c_str(), ios::out | ios::binary);

打印:

'myfile.tmp'
'myfile.tmp.bin'
terminate called after throwing an instance of 'std::ios_base::failure'
  what():  basic_ios::clear

该错误是什么意思,我该如何解决?

谢谢

最佳答案

取而代之的是:

fstream *binfile;

你应该这样做:

fstream binfile("filename", fstream::in | fstream::out | fstream::binary);

然后调用:

 binfile << "write here to file";

binfile.close();

这里不需要指针。

关于c++ - 打开二进制文件写入 : terminate called after throwing an instance of 'std::ios_base::failure' 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7420570/

相关文章:

c++ - 确定一个点是否在多面体内部

c++ - 使用设备文件符号链接(symbolic link)查找总线号和设备号

c++ - 关闭应用程序时忽略 Listcontrol 'DELETE_ALL_ITEMS' 事件

c++ - 双CPU内存分配性能

c++ - boost BGL : Edge property maps?

C++ 访问静态 constexpr 数组

c++ - 从中心顺时针扩展螺旋打印二维数组

c++ - 如何使用递归使用嵌套循环并检查 C++ 中的数字总和

c++ - 没有匹配的构造函数来初始化我的自定义分配器

c++ - 如何使对象深度只读