Does the FILE * file set equal to standard portable devices?

I have a function that should be able to write either to stdout or to a file, depending on what the user wants. However, it is default by default. To do this, I do the following (minus error checking, etc.):

FILE* out; if (writeToFile) { /*Code to open file*/; } else out = stdout; // ...rest of the function goes here if (out != stdout) fclose(out); 

This, of course, is a trick, but I have no idea how portable it is. And if it is not, and / or there is another problem with this, how can I do it?

+5
source share
2 answers

Yes, it is portable and it is great if you also don't bother with the low-level implementation of *stdout (for example, by calling close(fileno(stdout)) on Posix or using dup ).

+8
source

Everything should be fine. You may have problems if you pass the pointer back and forth along the border of the DLL (i.e., the C code outside the DLL passes the pointer to stdout to the C code inside the DLL), but in addition it must be portable.

+1
source

Source: https://habr.com/ru/post/952720/


All Articles