Giovanni Bajo - 2023-07-18

The problem appears to be that tmpfile() is implemented by msvcrt, which is unmaintained by Microsoft. tmpfile() in msvcrt is broken by design because it tries to create the temporary file in C:\ which is not writable by non-administrators. This is documented: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/tmpfile?view=msvc-170

The best workaround today appears to be the usage of mkstemp() which requires much more code: you need to create a pattern string in a writable buffer, call mkstemp, then convert the file descriptor into a FILE* using fdopen. Then, at the end, you need to remember to remove it after having closed it, which means that the writable buffer containing the filename must be kept around.

It would be good if mingw provided a working tmpfile() instead.