C++流创建和写入文件问题
C++ ofstream create and write file issue
我的虚拟文件系统提取器有问题。
ofstream ofs(path, ios::out|ios::binary);
ofs.write(file, length);
ofs.close();
路径是例如"data/char/actormotion.txt",应该在目录data/char/文件中创建名为actormotion的文件.txt但什么也没做。
首先检查它是否打开
std::ofstream ofs(path, ios::out|ios::binary);
if (ofs.is_open())
{
// write stuff
ofs.close();
}
else
{
std::cout << "Error opening file";
}
更有可能的是文件未打开。您可以使用bool is_open()
方法进行检查。
打开流后,建议检查其状态以确保一切顺利:
ofstream ofs(path, ios::out|ios::binary);
if ( (ofs.rdstate() & std::ofstream::failbit ) != 0 ){
//stream opened successfully, do the stuff...
在Windows系统中,您可以立即调用GetLastError()
来查询错误代码。
相关文章:
- 使用mongocxx驱动程序时包含头文件问题
- 生成文件问题
- 从文件问题加载纹理 Android NDK 和 OpenGL
- Xcode 11 头文件问题
- 使用 c++ ifstream 读取文本文件问题
- 单独的类声明和方法定义文件问题
- C++生成文件问题
- 生成文件问题(体系结构x86_64的未定义符号:"_main",引用自:主可执行文件的隐式入口/启动)
- C++删除未使用的库的生成文件问题
- C++ 使文件问题
- 阅读矩阵市场文件C++问题
- 查找第一个文件问题无法使任何示例正常工作。
- "Recursive Makefile Considered Harmful"样式生成文件问题
- 标题文件问题中的类定义
- 嵌套 while 循环,包含文件 c++ 问题
- 几个制作文件问题
- 生成文件问题,获取"目标'rgbapixel.o'的配方失败"
- 从yaml文件问题opencv读取数据
- 类成员C++标头和 .cc 文件问题
- 添加选项以显示百分比时出现生成文件问题