fwrite读写大量数据出现的错误

今天在制作游戏客户端更新器的时候出现问题。几乎所有的机器上都正常运行,只有一台机器运行后数据会乱,而且不是每次都出现,经过仔细的调试,发现问题出在了fwrite上。先看fwrite的定义。

============================================================
size_t fwrite(
   const void *buffer,
   size_t size,
   size_t count,
   FILE *stream 
);
Parameters
buffer

Pointer to data to be written.

size

Item size in bytes.

count

Maximum number of items to be written.

stream

Pointer to FILE structure.

Return Value

fwrite returns the number of full items actually written, which may be less than count if an error occurs. Also, if an error occurs, the file-position indicator cannot be determined. If either stream or buffer is a null pointer, the function invokes the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, this function sets errno to EINVAL and returns 0.

======================================================

fwrite的返回值即实际写入文件的字节数。此返回值与要写入的字数不符。通过errno宏得到的错误码是22, 即。

EINVAL

Invalid argument. An invalid value was given for one of the arguments to a function. For example, the value given for the origin when positioning a file pointer (by means of a call tofseek) is before the beginning of the file.

在网上查找,终于找到一个外国朋友写的帖子,问题描述基本一样,并且他们做了很多的实验,发现在低转数的硬盘上容易(不是一定)出现。而且和写入的数据大小也有关,当写入的数据较大,容易出现,当降低写入的数据大小,出现的概率随之降低。据此,我将要写入文件的数据分成小块儿分次使用fwrite写入文件。最终问题得到了解决!发帖以供以后遇到此问题的朋友参考。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值