1

In C, to specify how you open a file, you use a string, like "r", "w", etc. Why not an enum? Enums can be used with a switch statement, so then they would run a lot faster. Just why does fopen use a string instead of an enum? The question is simple.

  • 1
    But the file mode is not simply one character - there are multiple combinations allowed, like "wb" or "ta+". – Adrian Mole Dec 30 '19 at 03:40
  • @AdrianMole Are you giving OP fuel? Those are most likely compiletime optimizations anyway. – Ted Lyngmo Dec 30 '19 at 03:42
  • 4
    The question is essentially unanswerable; it was a decision made in the mid to late 70s that has persisted ever since. The `open()` call of that era was very simple with very few options; it now has a myriad options (POSIX [`open()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html) lists 18 options). Strings are very flexible and more easily extended if that's necessary, but of course the standard doesn't change much (check out Annex K of C11 [§K.3.5.2.1 The `fopen_s` function](http://port70.net/~nsz/c/c11/n1570.html#K.3.5.2.1)). – Jonathan Leffler Dec 30 '19 at 03:44
  • @Ted Actually, I voted-to-close (opinion-based) when I came across this in the F/P Review queue! However, there is something a bit more subtle about the "mode" string (see posted answer). – Adrian Mole Dec 30 '19 at 03:44
  • 1
    "a lot faster" is a misunderstanding. There are multiple orders of magitude between the possible performance difference here and the time cost of the operation `fopen` performs. – R.. GitHub STOP HELPING ICE Dec 30 '19 at 03:51
  • @JonathanLeffler in fact the question is answerable based on e.g. C99 rationale (see my answer); your comment has a hint. – Antti Haapala -- Слава Україні Dec 30 '19 at 07:37
  • 1
    @JonathanLeffler lol, you've written an [answer](https://stackoverflow.com/a/2523898/918959) to this already. – Antti Haapala -- Слава Україні Dec 30 '19 at 07:48
  • 2
    @AnttiHaapala — I don't remember every answer I've written, I'm afraid — ten years on SO is a long time! – Jonathan Leffler Dec 30 '19 at 08:22

1 Answers1

0

Because fopen can be called with a combination of flags, example:

FILE *fp = fopen("MyFile", "wb+");

You can achieve something similar by |'ing together binary flags, an alternate interface might look like this:

FILE *fp = fopen("MyFile", F_WRITE | F_BINARY | F_UPDATE);

Or something like that, but that's rather clunkier than using a string isn't it?

In any case, enums aren't suitable because you'd have to have a unique enum for every possible combination of options, which is definitely clunkier and harder to code an implementation for.

nickelpro
  • 2,537
  • 1
  • 19
  • 25
  • 2
    No, you don't have to have separate enums for each possible combination; you can use `enum { O_RDONLY = 0, O_WRONLY = 1, O_RDWR = 2, O_EXCL = 4, O_TRUNC = 8, … };` and then combine those values as you showed. Note that `O_RDONLY` is traditionally 0 and is not combinable with `O_WRONLY` to get `O_RDWR`, but the rest of the values associated with `open()` could be an enumeration — except that POSIX requires that the _values shall be bitwise-distinct and shall be suitable for use in `#if` preprocessing directives_ and enumerations are not suitable for `#if`. – Jonathan Leffler Dec 30 '19 at 03:49
  • @JonathanLeffler I interpreted the question to mean "Why don't we just enumerate the possible mode inputs?" Yes you can implement flags using an enum, and in fact that's usually the best way. – nickelpro Dec 30 '19 at 03:53
  • @nickelpro posix `open` has this interface, so not only it is possible, but it is implemented on one of the most prevalent APIs – bolov Jan 15 '20 at 12:51