Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

file

mode

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

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);
    }
like image 961
Anh Minh Tran Avatar asked May 04 '17 08:05

Anh Minh Tran


People also ask

What is WB file mode?

The wb indicates that the file is opened for writing in binary mode. When writing in binary mode, Python makes no changes to data as it is written to the file.

What is the difference between Rb+ and WB+?

rb+ Opens a file for both reading and writing in binary format. wb+ Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, it creates a new file for reading and writing.

What is WB mode in C?

“wb” – Open for writing in binary mode. If the file exists, its contents are overwritten. If the file does not exist, it will be created.


2 Answers

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

Most common translation is for end of line: \n is translated to \r\n in Windows.

like image 54
pmg Avatar answered Oct 10 '22 04:10

pmg


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

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.)

So, it stands for binary 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. For files that should be shared and/or accessed "later", this is not the proper way to do it in C. Look into serialization.

like image 39
unwind Avatar answered Oct 10 '22 05:10

unwind