Home > other >  Reading a file with a wrong access mode in C
Reading a file with a wrong access mode in C

Time:06-22

In a project I'm writing a file as binary, using fopen(p_full_path, "ab"); before I start writing it.

When I read it, I noticed I was doing this in a wrong way: to open the file I used fopen(p_full_path, "r "); instead of fopen(p_full_path, "rb ");.

The content of the file is correct anyway. So I don't understand if there is a real difference between the modes r and rb , I mean if there is the possibility of reading the wrong content because I'm not reading the file as binary.

Reading the documentation of fopen() I didn't find the answer.

CodePudding user response:

The answer to your question depends on which platform you are using.

For example, on POSIX-compliant platforms (such as Linux), there is no difference between binary mode and text mode.

However, on Microsoft Windows, the situation is very different: In binary mode, lines are terminated by character sequences of \r\n (carriage return followed by line feed), whereas in text mode, they are only terminated by \n characters. This is because \r\n is automatically converted to \n in text mode, whereas in binary mode, no conversion is performed. Also, in text mode, the character code \x1A is interpreted as the end of the file, whereas in binary mode, this value has no special meaning and is treated as a value like any other.

  • Related