文件访问模式 "w" 和 "wb
The difference in File access mode "w" and "wb
这些代码块之间有什么不同。我试图搜索 "wb" 但没有在任何地方看到它。包含 "wb" 的文件来自我的导师
FILE *f = fopen(DB_FILE_NAME, "wb");
if (f == NULL) {
printf("Write error\n");
} else {
/* write n_students elements of the studentlist array */
fwrite(studentlist, sizeof(student_t), n_students, f);
fclose(f);
}
和
FILE *f = fopen(DB_FILE_NAME, "w");
if (f == NULL) {
printf("Write error\n");
} else {
/* write n_students elements of the studentlist array */
fwrite(studentlist, sizeof(student_t), n_students, f);
fclose(f);
}
绝对 任何 对 fopen()
函数的引用都会告诉您这一点。例如 manual page,这是类 Unix 环境中使用的常见文档:
The mode string can also include the letter 'b' either as a last character
or as a character between the characters in any of the two-character
strings described above. This is strictly for compatibility with C89 and
has no effect; the 'b' is ignored on all POSIX conforming systems,
including Linux. (Other systems may treat text files and binary files
differently, and adding the 'b' may be a good idea if you do I/O to a
binary file and expect that your program may be ported to non-UNIX
environments.)
因此,它代表 binary,用于指示您打算将文件内容视为非文本内容。
对于您的代码,二进制访问似乎是正确的。但是,直接写入原始 struct
值通常是一个非常糟糕的主意,因为您不知道编译器使用的确切内部格式并且它可能会意外更改。对于应该共享 and/or 访问 "later" 的文件,这不是在 C 中执行此操作的正确方法。查看序列化。
在 fopen documentation 中:
With the mode specifiers above the file is open as a text file. In order to open a file as a binary file, a "b" character has to be included in the mode string. This additional "b" character can either be appended at the end of the string (thus making the following compound modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").
在访问模式中指定 "b"
可防止标准库(的某些实现)在 reading/writing 时将一些字符转换为文件。
最常见的翻译是行尾:\n
在 Windows 中翻译为 \r\n
。
这些代码块之间有什么不同。我试图搜索 "wb" 但没有在任何地方看到它。包含 "wb" 的文件来自我的导师
FILE *f = fopen(DB_FILE_NAME, "wb");
if (f == NULL) {
printf("Write error\n");
} else {
/* write n_students elements of the studentlist array */
fwrite(studentlist, sizeof(student_t), n_students, f);
fclose(f);
}
和
FILE *f = fopen(DB_FILE_NAME, "w");
if (f == NULL) {
printf("Write error\n");
} else {
/* write n_students elements of the studentlist array */
fwrite(studentlist, sizeof(student_t), n_students, f);
fclose(f);
}
绝对 任何 对 fopen()
函数的引用都会告诉您这一点。例如 manual page,这是类 Unix 环境中使用的常见文档:
The mode string can also include the letter 'b' either as a last character or as a character between the characters in any of the two-character strings described above. This is strictly for compatibility with C89 and has no effect; the 'b' is ignored on all POSIX conforming systems, including Linux. (Other systems may treat text files and binary files differently, and adding the 'b' may be a good idea if you do I/O to a binary file and expect that your program may be ported to non-UNIX environments.)
因此,它代表 binary,用于指示您打算将文件内容视为非文本内容。
对于您的代码,二进制访问似乎是正确的。但是,直接写入原始 struct
值通常是一个非常糟糕的主意,因为您不知道编译器使用的确切内部格式并且它可能会意外更改。对于应该共享 and/or 访问 "later" 的文件,这不是在 C 中执行此操作的正确方法。查看序列化。
在 fopen documentation 中:
With the mode specifiers above the file is open as a text file. In order to open a file as a binary file, a "b" character has to be included in the mode string. This additional "b" character can either be appended at the end of the string (thus making the following compound modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").
在访问模式中指定 "b"
可防止标准库(的某些实现)在 reading/writing 时将一些字符转换为文件。
最常见的翻译是行尾:\n
在 Windows 中翻译为 \r\n
。