Search code examples
cfile-iofopenhistoryc-standard-library

Why does fopen take a string as its second argument?


It has always struck me as strange that the C function fopen() takes a const char * as the second argument. I would think it would be easier to both read your code and implement the library if there were bit masks defined in stdio.h, like IO_READ and such, so you could do things like:

FILE *myFile = fopen("file.txt", IO_READ | IO_WRITE);

Is there a programmatic reason for the way it actually is, or is it just historic? (i.e. 'That's just the way it is.')


Solution

  • One word: legacy. Unfortunately we have to live with it.

    Just speculation: Maybe at the time a const char * seemed more flexible solution, because it is not limited in any way. A bit mask could only have 32 different values. Looks like a YAGNI to me now.

    More speculation: Dudes were lazy and writing "rb" requires less typing than MASK_THIS | MASK_THAT :)