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.

5
  • 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? Commented Sep 21, 2016 at 10:31
  • One distinction is that fseek() operates differently for the different modes Commented Sep 21, 2016 at 10:35
  • And one would generally use fread() and fwrite() with binary but fprintf(), fscanf(), fputc() and fgetc() with text Commented Sep 21, 2016 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()? Commented Sep 21, 2016 at 10:38
  • @Dan Getz - thanks, yes that answer was useful. Commented Sep 21, 2016 at 10:39

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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?
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.
Brilliant. Thank you.
In Unix, if one reads a file with text and a beginning byte-order-mark BOM, is that BOM read as a few characters?
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.