8

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.

Jongware
  • 22,200
  • 8
  • 54
  • 100
lychee
  • 1,771
  • 3
  • 21
  • 31
  • 4
    For unix there is no difference. (text vs binary is merely a Microsoft artefact) – wildplasser Sep 13 '14 at 12:39
  • 1
    Not only, AFAIK it is in C99 standard specification. – Basile Starynkevitch Sep 13 '14 at 12:40
  • 2
    Not all "text" files *will* be read correctly with CR/LF translation; only those encoded in ASCII (or a close derivate or extension) will be. Arguably, a file with text encoded in EBCDIC is "a text file", so this at least needs some definition of the used encoding. Also, CR/LF translation can mess up 16-bit Unicode text files. One could state a separate "text mode" has effectively been rendered obsolete. – Jongware Sep 13 '14 at 13:19
  • 1
    @wlidplasser: I thought that some old versions of MacOS also care about binary vs text ? (But I don't know MacOS9) – Basile Starynkevitch Sep 13 '14 at 13:25
  • 1
    I also recall that Apple once had `\n\r`, or `\r` only as line separator. – wildplasser Sep 13 '14 at 13:58
  • 2
    IIR, another difference some systems had in binary vs. text mode was the `EOF`. In text mode, a `'^Z'` would not be read as a `char`, but would kick of the `EOF` condition. A `'^Z'` would be applied when writing a file and closing it. Even one old system differentiate its line buffering on text/binary mode - but I doubt it was very C compliant. A number of OS's did (still do?) differentiate. If you want your code to be incompatible with non-*nix systems, ignore the text/binary setting. – chux - Reinstate Monica Sep 13 '14 at 17:15

1 Answers1

6

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)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547