C fopen mode parameter

Why is the 'mode' parameter for fopen in C given by a string? It would be more reasonable (in my way of thinking) that it be a bitmask or the like. The overhead that is required is inefficient and unnecessary.

+4
source share
2 answers

C11 Β§7.21.5.3 fopen function

The mode argument points to a string. If the line is one of the following, open the file in the specified mode. Otherwise, the behavior is undefined.271)

In a footnote:

271) If a line starts with one of the above sequences, the implementation may prefer to ignore the remaining characters or use them to select different file types (some of which may not correspond to the properties in 7.21,2

According to the C99 Justification, the committee considers that the implementation can choose mode , except for the flags:

Justification for an International Standard - Programming Languages ​​- C Β§7.19.5.3 fopen Function

An implementation may allow additional file specifications to be allowed as part of the mode line argument. For instance,

 file1 = fopen(file1name, "wb,reclen=80"); 

may be a reasonable extension to a system that provides writable binary files and allows the programmer to specify the length of the record.

GNU libc has an extension that allows mode contain ccs=STRING , see the glibc manual

+3
source

If it were a bitmask, it would be more limited by future expansion. The GNU C library already allows you to use 10 different modes and MSVC 15. In addition, they support the syntax ,ccs=string , which is not possible with a bitmask

+1
source

All Articles