Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between text mode and binary mode in c

Tags:

c

file-io

while writing/reading of file in text mode, the new line character is translated in to carriage return and linefeed i.e \n to \r\n but in binary mode this doesn't happen.

Similarly, the ASCII value 26 will be written at the end of file in text mode but this doesn't happen in binary mode.

I know this question was asked earlier in SO but there i didn't find any reasoning for this behavior.

I mean, is this behavior is just to differentiate the text and binary mode or is there any specific reason of this translation and not writing ASCII value 26 in case of binary mode.

like image 673
facebook-100001358991487 Avatar asked Dec 26 '22 13:12

facebook-100001358991487


2 Answers

Text file handling is operating system dependent. Binary files are not processed at all. Windows, replaces line endings with CR+LF, and in Linux, and OSX it is LF. In Linux, there is no difference between text file and binary file processing when OS is concerned.

like image 31
perreal Avatar answered Mar 05 '23 03:03

perreal


In a sense, binary mode is "raw": nothing is translated, since it has no basis for doing so. Whereas in text mode, the file is interpreted as text, and thus (for example) line endings get translated to the appropriate representation.

like image 186
Scott Hunter Avatar answered Mar 05 '23 05:03

Scott Hunter