Avoid writing 0x0A as 0x0D0A: Be careful of File Access Mode

When dealing with file I/O in C/C++, we should first open a file with fopen:
FILE * fopen ( const char * filename, const char * mode )
The parameter mode refers to “File Access Mode“. You have the following options (from cplusplus.com):

“r” read: Open file for input operations. The file must exist.
“w” write: Create an empty file for output operations. If a file with the same name already exists, its contents are discarded and the file is treated as a new empty file.
“a” append: Open file for output at the end of a file. Output operations always write data at the end of the file, expanding it. Repositioning operations (fseek, fsetpos, rewind) are ignored. The file is created if it does not exist.
“r+” read/update: Open a file for update (both for input and output). The file must exist.
“w+” write/update: Create an empty file and open it for update (both for input and output). If a file with the same name already exists its contents are discarded and the file is treated as a new empty file.
“a+” append/update: Open a file for update (both for input and output) with all output operations writing data at the end of the file. Repositioning operations (fseek, fsetpos, rewind) affects the next input operations, but output operations move the position back to the end of file. The file is created if it does not exist.

Note: If you want to open it in “text” mode, add “t” at the end, such as rt, w+t;
If you want to open it in “binaty” mode, add “b” at the end, such as r+b, ab.
The default is in text mode.

Why an extra 0x0D appears?

Sometimes, you may notice that your program automatically writes 0x0A as 0x0D0A on Windows. It is because you open the file as a “text” file, not a “binary” file. There are some difference:
( In ASCII code, 0x0A is the “Line Feed” character \n while 0x0D is the “Carriage Return” character\r. )

  1. On Windows, \r\n is used to represent a new line in a text file. So, if one opens a file as “text”, fwrite / fputs adds \r in front of \n whenever it writes \n but there is nothing done when it writes \n in “binary” mode.
  2. On Unix / Linux, merely \n can represent a new line in a text file. So, there is no difference between “text” mode and “binary” mode.

Code for opening file in “text” mode and write “0x0A”:

Run the program and you will obtain the file “out_text.file”. If you open it using Hex Editor, you will see an extra 0x0D is added before 0x0A. 4 bytes were wrote but the file was in 5 bytes.

0x0D is added before 0x0A

Solution

You should open the file in “binary”. Here is the code for you to test:

You should now have no more 0x0D!

Comments

comments

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">