1

I am reading The C Programming Language (2nd Edition). On page 157 and 158 the author gives a code snippet of fopen in the Unix system. At the end of the snippet the author added:

In particular, our fopen does not recognize the "b" that signals binary access, since that is meaningless on UNIX systems, nor the "+" that permits both reading and writing.

Why does the author say it's meaningless? (The "b" and "+" mentioned here are file access modes)

8

Some non-Unix systems treat binary and text files in different ways. For example under DOS, Windows and OS/2 (which wouldn’t have been relevant when fopen was designed, but serve as useful examples), opening a file in text mode and writing to it will convert line endings from “C” convention (\n) to whatever the platform requires. On other systems, opening a file in binary mode will cause it to be processed in records. This is what fopen’s “b” flag controls: files opened without it are opened in text mode, files opened with it are opened in binary mode. Since Unix-style systems don’t have this distinction, “b” is ignored (and doesn’t cause an error).

My copy of the book doesn’t mention “+”, but I’m guessing fopen didn’t support it then (it does now).

4

According to the POSIX specification of fopen() (which did not exist when that book was written):

The character b shall have no effect, but is allowed for ISO C standard conformance.

The b mode must be accepted, but it has no effect on a POSIX system.

Furthermore, the + mode:

When a file is opened with update mode (+ as the second or third character in the mode argument), both input and output may be performed on the associated stream.

So that definitely has an effect even on a POSIX system.

Note that the C programming language has changed a bit since the K&R book was written.

  • 1
    POSIX wasn’t a thing when K&R wrote their book... – Stephen Kitt Mar 13 '18 at 14:16
  • 1
    @StephenKitt Ah, yes, but the question wonders why they (the b and + modes) are meaningless. – Kusalananda Mar 13 '18 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.