fwrite 不会在 C 中保存完整数组
我有一个大小为 668x493 的数组,我想保存它。所以我正在做以下事情。
data :是指向保存值的数组的指针。
long lSize;
FILE* image_save;
image_save=fopen("image_save_file.bin","w+b");
fwrite(data,1,329324,image_save);
然而,当我读回这个数组时:
char* check_image;
p1File=fopen("image_save_file.bin","r+b");
fseek (p1File , 0 , SEEK_END);
lSize = ftell (p1File);
fseek (p1File , 0 , SEEK_SET);
当我检查 lSize 时,我看到它 327680 ???
所以当然,当我害怕时,我只得到 327680 个值!
请问,你能指出我的错误吗?
I have an array of size 668x493 which i want to save. So i am doing the following.
data : is a pointer to an array that holds the values.
long lSize;
FILE* image_save;
image_save=fopen("image_save_file.bin","w+b");
fwrite(data,1,329324,image_save);
However, when i read back this array:
char* check_image;
p1File=fopen("image_save_file.bin","r+b");
fseek (p1File , 0 , SEEK_END);
lSize = ftell (p1File);
fseek (p1File , 0 , SEEK_SET);
when i check lSize, i see it 327680 ???
So of course when i do fread i get only 327680 values !
Kindly asking, can you pinpoint my mistake ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有趣的是,327680 是 4096 的精确倍数 (80 * 4096)。
在读回数据之前是否刷新/关闭输出文件?
Interestingly, 327680 is an exact multiple of 4096 (80 * 4096).
Are you flushing/closing the output file before you read the data back in?
fwrite()
函数被缓冲。尝试刷新文件流上的数据,然后重试。The
fwrite()
function is buffered. Try flushing the data on the file stream and try again.fwrite 返回一个 int 值,表示实际写入的字节数。仔细检查以确保这与预期不同(几乎肯定会如此)。然后,您可以使用 perror 打印出正在发生的错误。
fwrite returns an int indicating the actual number of bytes written. Double check to make sure this differs from expected (it almost certainly does). Then, you can use perror to print out the error that's occurring.