Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C |
both |
#include <stdio.h>
FILE *freopen(const char *__restrict__filename, const char *__restrict__mode,
FILE *__restrict__stream);
Closes the file currently associated with stream and pointed to by stream, opens the file specified by the filename, and then associates the stream with it.
The freopen() function opens the new file with the type of access requested by the mode argument. The mode argument is used as in the fopen() function. See fopen() — Open a file for a description of the mode parameter.
You can also use the freopen() function to redirect the standard stream files stdin, stdout, and stderr to files that you specify. The file pointer input to the freopen() function must point to a valid open file. If the file pointer has been closed, the behavior is undefined.
freopen("a.b","wb,type=memory",stdout);
If filename is an empty string, freopen() closes the file and reuses the original file name. For details on how the file name and open mode is interpreted, see z/OS XL C/C++ Programming Guide.
A standard stream can be opened by default to a type of file not available to a general fopen(). This is true for standard streams under CICS®, and also true for the default stderr, when running a non-POSIX Language Environment application.
fp = freopen("", "rb", stdin);
Large file support for z/OS UNIX files: Large z/OS UNIX files are supported automatically for AMODE 64 C/C++ applications. AMODE 31 C/C++ applications must be compiled with the option LANGLVL(LONGLONG) and define the _LARGE_FILES feature test macro before any headers are included to enable this function to operate on z/OS UNIX files that are larger than 2 GB in size. File size and offset fields are enlarged to 63 bits in width. Therefore, any other function operating on the file is required to define the _LARGE_FILES feature test macro as well.
If successful, freopen() returns the value of stream, the same value that was passed to it, and clears both the error and EOF indicators associated with the stream.
A failed attempt to close the original file is ignored.
If an error occurs in reopening the requested file, freopen() closes the original file, and returns a NULL pointer value.
#include <stdio.h>
int main(void)
{
FILE *stream, *stream2;
stream = fopen("myfile.dat","r");
stream2 = freopen("", "w+", stream);
}
#include <stdio.h>
int main(void)
{
FILE *stream, *stream2;
stream = fopen("myfile.dat","r");
stream2 = freopen("myfile2.dat", "w+", stream);
}