简体   繁体   English

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

[英]The difference in File access mode "w" and "wb

What is the different between these blocks of code.这些代码块之间有什么不同。 I tried to search for "wb" but don't see it anywhere.我试图搜索“wb”,但没有在任何地方看到它。 The file containing "wb" is from on of my tutors包含“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);
    }

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: \n is translated to \r\n in Windows.最常见的翻译是行尾: \n \r\n

Absolutely any reference on the fopen() function would have told you this.绝对任何关于fopen()函数的参考都会告诉你这一点。 For instance the manual page which is the common documentation used in Unix-like environments:例如,手册页是类 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.模式字符串还可以包括字母“b”作为最后一个字符或作为上述任何双字符字符串中字符之间的字符。 This is strictly for compatibility with C89 and has no effect;这只是为了与 C89 兼容,没有任何效果; the 'b' is ignored on all POSIX conforming systems, including Linux. 'b' 在所有符合 POSIX 的系统(包括 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.) (其他系统可能会以不同方式处理文本文件和二进制文件,如果您对二进制文件执行 I/O 操作并希望您的程序可以移植到非 UNIX 环境,那么添加 'b' 可能是个好主意。)

So, it stands for b inary and is useful to indicate that you intend to treat the contents of the file as not being text.因此,它代表二进制,用于指示您打算将文件的内容视为非文本。

For your code, binary access seems right.对于您的代码,二进制访问似乎是正确的。 However, directly writing raw struct values is generally a very bad idea, since you don't know the exact internal format used by the compiler and it can change unexpectedly.但是,直接编写原始struct值通常是一个非常糟糕的主意,因为您不知道编译器使用的确切内部格式并且它可能会意外更改。 For files that should be shared and/or accessed "later", this is not the proper way to do it in C. Look into serialization.对于应该“稍后”共享和/或访问的文件,这不是在 C 中执行此操作的正确方法。查看序列化。

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.为了将文件作为二进制文件打开,“b”字符必须包含在模式字符串中。 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”字符可以附加在字符串的末尾(从而产生以下复合模式:“rb”、“wb”、“ab”、“r+b”、“w+b”、“a +b") 或插入字母和混合模式的“+”符号之间(“rb+”、“wb+”、“ab+”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 文件访问中的文本和二进制模式有什么区别吗? - Is there any difference between text and binary mode in file access? 在Mac上的Xcode中使用fopen(fileName,“ wb”)之后无法访问我的二进制文件 - Can't access my binary file after using fopen(fileName, “wb”) in Xcode on Mac C-创建文件时使用“ w +”模式 - C - using the “w+” mode when creating a file 如果 fprintf 写入一个字符串,而 fwrite 写入字节; 那么,fopen 的参数“w”和 wb 用于什么? - If fprintf writes a string and fwrite writes bytes; then, for what is used fopen’s parameter “w” and wb? 使用fopen(fp,“wb”)创建用户输入的任何文件的副本 - Create a copy of any file the user inputs using fopen(fp,“wb”) 在 C 中读取具有错误访问模式的文件 - Reading a file with a wrong access mode in C 我可以获得`FILE *`的访问模式吗? - Can I get the access mode of a `FILE*`? 无法访问外部文件中定义的数组,W / O关键字“extern” - cannot access array defined in external file, W/O the keyword “extern” 在c中写入文件时,二进制模式和文本模式之间在性能上有区别吗? - is there performance difference between binary and text mode while writing to a file in c? 使用 C 从文件中读取字符串。使用 w+ 模式的 Fopen 不起作用 - Reading a string from a file with C. Fopen with w+ mode is not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM