1

I am learning C from the K&R handbook. I know (and read) that 'b' in a fopen call signals binary access. The book then goes on to say that this (binary access) is meaningless on UNIX systems.

The inference is that it is meaningful on Windows.

My question is, why does Windows make this distinction between text and binary and UNIX doesn't. Also, what is the distinction?

I found some things that come close to answering it, but still don't quite satisfy me:

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

http://perlmaven.com/what-is-a-text-file

Thanks in advance.

Community
  • 1
  • 1
Aidenhjj
  • 1,249
  • 1
  • 14
  • 27
  • Making a function argument meaningless is simple; one can just ignore it and do the same thing whether it's there or not. It sounds like you're asking more about what binary access means on Windows? – Dan Getz Sep 21 '16 at 10:31
  • One distinction is that `fseek()` operates differently for the different modes – Toby Sep 21 '16 at 10:35
  • And one would generally use `fread()` and `fwrite()` with binary but `fprintf()`, `fscanf()`, `fputc()` and `fgetc()` with text – Toby Sep 21 '16 at 10:36
  • @Toby OK. So does UNIX always treat files as binary? If you're writing code for UNIX, are you saying that you'd generally use `fread()` and `fwrite()`? – Aidenhjj Sep 21 '16 at 10:38
  • @Dan Getz - thanks, yes that answer was useful. – Aidenhjj Sep 21 '16 at 10:39

1 Answers1

2

Traditional windows system have different representation of text and binary files. Text files have a end of file marker (^-Z marker), so you can't read that character if you open it in text mode, IO layer will just detect it and returns you that you are at the end of file. In binary mode, it behaves like Unix systems.

Unix doesn't take care about file content, and provide no such semantic. On Unix, end of file, is just a try to read something past the length of the stream. This is why text or binary mode doesn't matters in Unix world, any character can appear in any kind of regular file.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • How does UNIX know that it's tried to read past the length of the string? Is the file / data length encoded in the file address somehow? – Aidenhjj Sep 21 '16 at 10:56
  • Each file has associated metadata where the length is, as last modification date, owner, access rights, etc. So if the current position of the opened stream is past the length, you are at the end of file. – Jean-Baptiste Yunès Sep 21 '16 at 10:57
  • Brilliant. Thank you. – Aidenhjj Sep 21 '16 at 10:58
  • In Unix, if one reads a file with text and a beginning byte-order-mark [BOM](https://en.wikipedia.org/wiki/Byte_order_mark), is that BOM read as a few characters? – chux - Reinstate Monica Sep 21 '16 at 14:25
  • Of course... BOM is just a sequence of bytes, as any other. Applications may interpret them as something special, some other don't bother. At least the kernel makes no difference. – Jean-Baptiste Yunès Sep 21 '16 at 14:35