freopen_s
Go Up to stdio.h Index
Header File
stdio.h
Category
File access functions
Prototype
errno_t freopen_s(FILE * restrict * restrict newstreamptr, const char * restrict filename, const char * restrict mode, FILE * restrict stream);
Description
Replaces freopen adding security enhancements.
freopen_s substitutes the open stream with the named file. It closes the stream regardless of whether the opening succeeded. freopen_s is useful for changing the file attached to stdin, stdout, or stderr. To do this, it first tries to close any file associated with the stream parameter, then opens the file named by filename, and associates the stream identified by the stream parameter with it.
If the file was opened successfully, the value pointing to the file is set to the newstreamptr parameter. Otherwise, newstramptr is null.
The mode string can have the same values as those for freopen, with the same meaning.
In case of an access violation, freopen_s does not close or try to open any file.
Return Value
On successful completion, freopen_s returns zero, otherwise it returns a nonzero value.
Example
#include <stdio.h> #include <stdlib.h> int main(void) { /* Redirect stdout */ FILE *file; file = new FILE; if (freopen_s(&file,"OUTPUT.FIL", "w", stdout)){ fprintf(stderr, "error redirecting stdout\n"); exit(1); } /* This output will go to a file */ printf("This will go into a file."); /* Close the standard output stream */ fclose(stdout); return 0; }
Portability
POSIX | Win32 | ANSI C | ANSI C++ | |
---|---|---|---|---|
freopen_s |
+ |
+ |
+ |
+ |