gpt4 book ai didi

c - fread/fwrite 将 size 和 count 作为参数的基本原理是什么?

转载 作者:太空狗 更新时间:2023-10-29 16:15:21 24 4
gpt4 key购买 nike

我们在工作中讨论了为什么 fread()fwrite() 为每个成员获取一个大小并计算并返回读取/写入的成员数量,而不是而不仅仅是获取缓冲区和大小。我们可以想出的唯一用途是,如果你想读/写一个结构数组,这些结构不能被平台对齐整除,因此已经被填充,但不能如此常见以保证这个选择在设计中。

来自 fread(3) :

The function fread() reads nmemb elements of data, each size bytes long,from the stream pointed to by stream, storing them at the location givenby ptr.

The function fwrite() writes nmemb elements of data, each size byteslong, to the stream pointed to by stream, obtaining them from the locationgiven by ptr.

fread() and fwrite() return the number of items successfully read or written(i.e., not the number of characters). If an error occurs, or theend-of-file is reached, the return value is a short item count (or zero).

最佳答案

fread(buf, 1000, 1, stream)fread(buf, 1, 1000, stream) 的区别在于,在第一种情况下你得到只有一个 1000 字节的 block 或什么都没有,如果文件较小,在第二种情况下,文件中的所有内容都小于或最多 1000 字节。

关于c - fread/fwrite 将 size 和 count 作为参数的基本原理是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/295994/

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