freopen
From cppreference.com
| Defined in header <stdio.h>
|
||
| (until C99) | ||
| (since C99) | ||
Reassigns an existing file stream stream to a different file identified by filename using specified mode. mode is used to determine the new file access mode.
Contents |
[edit] Parameters
| filename | - | file name to associate the file stream to | |||||||||||||||||||||||||||||||||||||||||||||
| mode | - | null-terminated character string determining new file access mode
| |||||||||||||||||||||||||||||||||||||||||||||
| stream | - | the file stream to modify | |||||||||||||||||||||||||||||||||||||||||||||
[edit] Return value
stream on success, NULL on failure
[edit] Example
freopen with error checking. The following code redirects stdout to a file
Run this code
#include <stdio.h> #include <stdlib.h> int main(void) { printf("stdout is printed to console"); if (freopen("redir.txt", "w", stdout) == NULL) { perror("freopen()"); fprintf(stderr,"freopen() failed in file %s at line # %d\n", __FILE__,__LINE__-3); exit(EXIT_FAILURE); } printf("stdout is redirected to a file"); fclose(stdout); return EXIT_SUCCESS; }
Output:
stdout is printed to console
[edit] See also
| opens a file (function) | |
| closes a file (function) | |
| C++ documentation for freopen
| |