gpt4 book ai didi

c - 为什么在 Linux 中硬盘已满时 fwrite 不返回 0?

转载 作者:太空狗 更新时间:2023-10-29 11:45:50 24 4
gpt4 key购买 nike

当硬盘 100% 满时,fwrite(比如 1000 字节)返回 0 [失败,如预期]但是当硬盘空间很小时说 600Bytes ,然后 fwrite (1000 bytes) 不是返回 0 [失败] 但返回 300 字节,再次调用 fwrite 仍然返回 300 字节,fwrite 永远不会失败,即使我们调用 100 次?

错误号正确设置为 28。我的问题是为什么 fwrite 的行为?这是正确的吗?如果 fwrite 返回的字节少于我们要写入的字节,那么这意味着磁盘已满?

处理这种情况的建议?

最佳答案

来自fwrite man page :

On success, fread() and fwrite() return the number of items read or written. This number equals the number of bytes transferred only when size is 1. If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).

因此,短写入表示错误,应查询 errno 以了解写入未完成的原因。在咨询了errno之后,Mat的回答解释了(和C标准说的基本一样,除了它使用了术语indeterminate),错误后文件位置无效,所以尝试在失败后继续使用它并没有很好的定义。

关于c - 为什么在 Linux 中硬盘已满时 fwrite 不返回 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16508305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com