Why can't I "freopen" a "tmpfile"? Why can't I "freopen" a "tmpfile"? docker docker

Why can't I "freopen" a "tmpfile"?


From the C99 standard:

The freopen function opens the file whose name is the string pointed to by filenameand associates the stream pointed to by stream with it. The mode argument is used justas in the fopen function.

If filename is a null pointer, the freopen function attempts to change the mode ofthe stream to that specified by mode, as if the name of the file currently associated withthe stream had been used. It is implementation-defined which changes of mode arepermitted (if any), and under what circumstances.

So, probably who wrote this code meant to change the temporary file open mode from w+b to r+ (which mostly boils down to change the stream to text mode). Unfortunately, it seems that in your implementation it's not possible to change the open mode of a temporary file in that way.

I suppose that it may come from the fact that closing a temporary file also deletes it, but it may also be that glibc implementation of freopen doesn't support mode changes in freopen (the manpage doesn't even mention the possibility to pass NULL as first argument).