freopen

Full prototype:

FILE* freopen(const char* restrict, const char* restrict, FILE* restrict);  

Purpose and Notes:

This function is essentially fclose() and fopen() combined together. It closes the stream pointed to by the third argument. It then opens the file passed in as the first argument according to the mode passed in as the second argumnet. It returns the file* back.

This function is scarcely defined in the C stanadard meaning that this is an "IMPLEMENTATION HELL" function. There are lots of gotchas and restrictions. In my personal opinion this function seems only useful in changing the standard streams such as stdin, stdout, and stderr to point to other files then they originally do.

The ability to point the standard streams can still be fairly useful in some cases. For example, if you are working with another software you do not have access to the source code and it has hardcoded functionality that is taking in from stdin you can point stdin to a file of your choice and now use the file as input instead during the execution of your program.

It is super implementation-defined what modes you can even change the new file to which can essentially render this function pointless in some implementations.

To be honest- this function seems pretty useless on GNU and POSIX systems as well as many others.

Returns:

FILE*- The file pointer passed in as the third argument or NULL if it failed.

Argument 1:

const char* restrict- The path to the new file you want to associate the stream (third argument with).

Argument 2:

const char* restrict- The mode you want to associate with the stream now with the file. (VERY IMPLEMENTATION DEFINED WHAT IS ALLOWED HERE)

Argument 3:

FILE* restrict- The stream/FILE pointer you want to associate with the file passed in as the first argument.

Example 1
  1    FILLER SOURCE CODE
FILLER TERMINAL