Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between text and binary mode in file access?

Is there any difference if I open a file in text mode rather than binary mode? Because I read that UNIX and Linux make no distinction between text and binary files.

like image 873
lychee Avatar asked Sep 13 '14 12:09

lychee


People also ask

What is the difference between binary and text file?

Text files are organized around lines, each of which ends with a newline character ('\n'). The source code files are themselves text files. A binary file is the one in which data is stored in the file in the same way as it is stored in the main memory for processing.

Which is better binary file or text file?

Text files are more restrictive than binary files since they can only contain textual data. However, unlike binary files, they are less likely to become corrupted. While a small error in a binary file may make it unreadable, a small error in a text file may simply show up once the file has been opened.

What is binary mode in file?

Binary mode allows programmers to manipulate files byte by byte rather than in larger logical structures.

What is the difference between text and binary files class 12?

Binary file contains the data in the form of 0 and 1(series of binary values) and text files contains the data in the form of stream of characters.In general binary files are identified as executable files. But binary files are not in readable form as like text file.


1 Answers

There is no difference on Linux (at least on native file systems like Ext4 and on most other file systems too, with the usual GNU libc).

Perhaps some bizarre filesystems could have a specific flag to open differently binary or text files. I know no such filesystem. Maybe you could code some FUSE filesystem making the distinction, perhaps with some additional hack around fopen inside a bizarrely customized libc

However, C99 standard (at least page 271, §7.19.5.3 of n1256 draft) mentions explicitly the text vs binary mode, so your program would be easier to port to other systems (such as Windows) if it conforms to the standard.

So my point is that you might want to try passing a mode string to fopen which differentiate the binary vs text mode. (I agree that I don't do that very often). It won't hurt.

The Linux fopen(3) man page explicitly says:

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

Of course, the open(2) syscall does not have any way of transmitting a mode flag. (You 'll need some private ioctl(2) probably)

like image 157
Basile Starynkevitch Avatar answered Oct 15 '22 16:10

Basile Starynkevitch