Thursday, 8 June 2017

file io - Why does C's "fopen" take a "const char *" 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's code 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.")


Answer



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 :)


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...