C语言 文件访问模式“w”和“wb”的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43777913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:39:25  来源:igfitidea点击:

The difference in File access mode "w" and "wb

cfileaccessmode

提问by Anh Minh Tran

What is the different between these blocks of code. I tried to search for "wb" but don't see it anywhere. The file containing "wb" is from on of my tutors

这些代码块之间有什么不同。我试图搜索“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);
    }  

and

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);
    }

采纳答案by unwind

Absolutely anyreference on the fopen()function would have told you this. For instance the manual pagewhich is the common documentation used in Unix-like environments:

绝对任何有关该fopen()功能的参考都会告诉您这一点。例如手册页,它是类 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.)

模式字符串还可以包括字母“b”作为最后一个字符或作为上述任何两个字符串中的字符之间的字符。这严格是为了兼容 C89,没有任何影响;'b' 在所有符合 POSIX 的系统上都被忽略,包括 Linux。(其他系统可能会以不同的方式对待文本文件和二进制文件,如果您对二进制文件进行 I/O 操作并希望您的程序可以移植到非 UNIX 环境,则添加“b”可能是个好主意。)

So, it stands for binary and is useful to indicate that you intend to treat the contents of the file as not being text.

因此,它代表了binary并且是有用的,以表明您打算在处理该文件的内容不是文字。

For your code, binary access seems right. However, directly writing raw structvalues is generally a very bad idea, since you don't know the exact internal format used by the compiler and it can change unexpectedly. For files that should be shared and/or accessed "later", this is not the proper way to do it in C. Look into serialization.

对于您的代码,二进制访问似乎是正确的。但是,直接写入原始struct值通常是一个非常糟糕的主意,因为您不知道编译器使用的确切内部格式,并且它可能会意外更改。对于应该“稍后”共享和/或访问的文件,这不是在 C 中执行此操作的正确方法。查看序列化。

回答by pmg

Specifying "b"in the access mode prevents (some implementations of) the standard library from translating a few characters when reading/writing to the file.

"b"在访问模式中指定可防止(某些实现)标准库在读取/写入文件时翻译几个字符。

Most common translation is for end of line: \nis translated to \r\nin Windows.

最常见的转换是行尾:在 Windows 中\n转换为\r\n

回答by Badda

In fopen documentation:

fopen 文档中

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”字符。这个额外的“b”字符可以附加在字符串的末尾(从而形成以下复合模式:“rb”、“wb”、“ab”、“r+b”、“w+b”、“a +b") 或插入字母和混合模式的“+”符号之间(“rb+”、“wb+”、“ab+”)。