c++ - fstream 写入文件

标签 c++ fstream

我需要一些帮助来使用 fstream 写入文件。我目前可以使用 fwrite 方法进行写入。但我想使用 fwrite 转换数据类型并使用它写入

下面是我目前要做的事情:

noOfRows = 700;
noOfCols = 100
array[0] = unsigned char [noOfRows * noOfCols]

fstream fstreamFile;
fstreamFile.open ("fstreamFile.txt", fstream::out);

for(int i = 0; i < noOfRows; i++ )
{
fwrite(array[0]+(i*noOfRows), sizeOf(unsigned char), noOfCols, fwriteFile); // writes ok
fstreamFile.write((char*) array[0]+(i*noOfCols), sizeof(unsigned char)); // doesn't write
}

有人可以告诉我我做错了什么吗?我没有遇到编译错误,只是文件没有被写入(我已经预先创建了它)。

最佳答案

2 事情,你在寻找文件的正确位置吗?它应该在工作目录所在的位置。您不需要事先创建文件,它会被创建。其次,确保它正在创建文件,您可以这样做:

if (!fstreamFile) { cout << "errorr.." << endl; }

关于c++ - fstream 写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11163319/

相关文章:

c++ - 编译错误/usr/bin/ld : cannot find -lpulse

c++ - C++读写二进制文件

c++ - fstream给我错误的文件大小

c++ - 使用 getline() 从文件发出读取行

c++ - seekg 不转到文件开头

c++ - 什么是应用足迹以及如何计算它?

python - 在 C++ 中使用 MinGW 嵌入 python3.6 链接失败

c++ - 为什么不常用算术表示?

c++ - std::cout 不适用于结构的重载 '<<' 运算符

c++ - 如何从 POSIX 文件描述符构造 c++ fstream?