gtpc2m39 | C/C++ Language Support User's Guide |
This function redirects an open file.
Format
#include <stdio.h> FILE *freopen(const char *filename, const char *mode, FILE *stream);
This function closes the file currently associated with stream and pointed to by stream, opens the file specified by 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 Table 4 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 parameter to the freopen function must point to a valid open file. If the file has been closed, the behavior is not defined.
You could use the following freopen call to redirect stdout to a file A.B:
freopen("A.B","w",stdout);
Normal Return
If successful, the freopen function 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.
Error Return
If an error occurs when reopening the requested file, the freopen function closes the original file and returns a NULL pointer value.
Programming Considerations
The TPF system does not support creating, updating, or deleting files in 1052 or UTIL state. Special files may or may not be writable in 1052 or UTIL state depending on the device driver implementation.
Examples
The following example closes the stream data stream and reassigns its stream pointer:
#include <stdio.h> int main(void) { FILE *stream, *stream2; stream = fopen("myfile.dat","r"); stream2 = freopen("myfile2.dat", "w+", stream); }
Related Information
See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.