fread和fwrite有失败吗?怎么处理?
当我使用fread(C语言)读取文件时,fread的返回值有时会是0。
按照手册建议:
fread() 和 fwrite() 返回成功读取的项目数或 写的
我必须写这样的代码吗?
int bytes_read;
while((bytes_read = fread(buffer, sizeof(int), 1, fp)) == 0) {
}
我们是否总是需要检查 fread 或 fwrite 是否成功?
when I read from a file using fread (C language), the return value of fread sometimes would be 0.
As manual suggested:
fread() and fwrite() return the number of items successfully read or
written
do I have to write code like this?
int bytes_read;
while((bytes_read = fread(buffer, sizeof(int), 1, fp)) == 0) {
}
do we always have to check whether fread or fwrite succeeded?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,如果
fread
或fwrite
返回的读取或写入的记录数少于预期的记录数,那么执行这样的重试循环是没有意义的。也就是说,stdio 不像低级的读取和写入操作那样会导致“短”读取或写入。如果
fread
返回的记录数少于请求的记录数,则可能遇到 EOF 或出现严重的读取错误。您可以通过检查feof()
和ferror()
来区分它们。同样,如果
fwrite
返回的记录数少于请求的记录数,则您要么用完磁盘空间,要么遇到严重的写入错误。无论如何,由于缓冲 stdio 使得基本上不可能知道成功写入了多少内容,因此如果遇到写入错误,通常需要考虑文件丢失并中止整个操作。
No, there's no sense in doing a retry-loop like this if
fread
orfwrite
returns fewer than the expected number of records read or written. That is to say, stdio is not like the low-levelread
andwrite
operations that can result in "short" reads or writes.If
fread
returns fewer than the requested number of records, you've either hit EOF or a serious read error. You can distinguish between them by checkingfeof()
andferror()
.Similarly, if
fwrite
returns fewer than the requested number of records, you've either run out of disk space or hit a serious write error.In any case, due to buffering stdio makes it essentially impossible to know how much was successfully written, so if you encounter a write error, you usually need to consider the file lost and abort the whole operation.
http://pubs.opengroup.org/onlinepubs/000095399/functions/fread.html
成功完成后,仅当遇到读取错误或文件结尾时,fread() 才会返回成功读取的元素数,该数小于 nitems。如果 size 或 nitems 为 0,则 fread() 应返回 0,并且数组的内容和流的状态保持不变。否则,如果发生读取错误,则应设置流的错误指示符,并设置 errno 来指示错误。
http://pubs.opengroup.org/onlinepubs/007904875/functions/fwrite.html
fwrite() 函数应返回成功写入的元素数量,如果遇到写入错误,该数量可能小于 nitems。如果 size 或 nitems 为 0,fwrite() 将返回 0 并且流的状态保持不变。否则,如果发生写入错误,则应设置流的错误指示符,并应设置 errno 来指示错误。
必须使用 Ferror() 或 feof() 函数来区分错误条件和结束- 文件状况。
http://pubs.opengroup.org/onlinepubs/000095399/functions/fread.html
Upon successful completion, fread() shall return the number of elements successfully read which is less than nitems only if a read error or end-of-file is encountered. If size or nitems is 0, fread() shall return 0 and the contents of the array and the state of the stream remain unchanged. Otherwise, if a read error occurs, the error indicator for the stream shall be set, and errno shall be set to indicate the error.
http://pubs.opengroup.org/onlinepubs/007904875/functions/fwrite.html
The fwrite() function shall return the number of elements successfully written, which may be less than nitems if a write error is encountered. If size or nitems is 0, fwrite() shall return 0 and the state of the stream remains unchanged. Otherwise, if a write error occurs, the error indicator for the stream shall be set,and errno shall be set to indicate the error
The ferror() or feof() functions must be used to distinguish between an error condition and an end-of-file condition.
是。
返回值应始终为计数。
如果不是 - 您应该使用
ferror()
或feof()
来确定是否已到达文件末尾和/或遇到错误。忽略错误和/或意外情况会导致不可靠的软件对毫无戒心的用户造成影响。
http://www.cplusplus.com/reference/clibrary/cstdio/fread/<-- 垃圾http://pubs.opengroup.org/onlinepubs/000095399/functions/fread.html
Yes.
The return value should always be count.
If it's not - you should use
ferror()
orfeof()
to determine whether you've reached the end of the file and/or encountered an error.Ignoring errors and/or unexpected conditions is the stuff from which unreliable software is wrought down on unsuspecting users.
http://www.cplusplus.com/reference/clibrary/cstdio/fread/<-- Junkhttp://pubs.opengroup.org/onlinepubs/000095399/functions/fread.html
如果 fread 失败,它通常会继续失败。通常是因为它到达了文件末尾,但也可能是由于其他原因。如果失败,您通常不会再尝试。
If fread fails, it will typically keep failing. Typically because it hit the end of file, but possibly for some other reason. If it fails, you would normally not try again.