fwrite() 如何在 c 文件中工作?

how fwrite() works in c files?

fwrite() 用于在c中向文件写入结构, 假设我们有以下结构:

struct student{
int rollno;
char name[20],grade;
float total;
};

fwrite 只能看到内存中的结构。它不知道字段的名称、它们的偏移量或任何相关信息。它所能做的就是将你的记忆直接写入文件。因此,它将写入字符串的所有 20 个字节,以及结构的其余部分。

如果您希望它以人类可读的形式编写内容,您必须编写一个了解每个字段并对其进行格式化的函数。

这是因为 C 不具备称为 "reflection" 的东西,它允许程序存储和利用诸如变量或字段的名称或类型等信息,并在运行时使用该信息。还有其他语言,比如 golang,可以做这样的事情。

fwrite 是一个 C 函数,它像其他函数一样处理结构,不做任何特殊处理(除非有要求)。它将该结构作为二进制对象,并写入任意数量的字节。

does it write full sizeof(struct tudent)bytes to the file every time we write it with fwrite()?

它会写入您要求 fwrite 写入的尺寸,因此如果它是 sizeof(structX),它会写入该尺寸。同样的操作fwrite两次,就会写两次。

if so then does it write name with 20 bytes of storage on the file even if you have only stored few characters in the name?

name 被声明为 char[20],占用 20 个字节。所以是的,那部分在磁盘上占用 20 个字节(即使 "string" 包含 "only" 3 个字符(即 '[=20=]' 位于第 4 个位置),所有 20 个字节都被写入。

if that is the case then can we modify the the written record using fread() with other functions such as fputs() fputc() putw(), but these function doesnt seem to modify the record correctly?

putwword 写入流,这意味着 int 原样(二进制字,不是它的字符串表示),但更好按照 man page 中的建议使用 fwritefputc 写一个独特的字符。 fputs 写入一个以 null 结尾的字符串,与 fwrite 不同,它写入您需要的字符数,而不管其中某处是否隐藏了零字节。