freopen - Untitled
Description: Untitled

Next: fseek, Previous: fread, Up: Stdio

### 4.25 `freopen`—open a file using an existing file descriptor

Synopsis

         #include <stdio.h>
         FILE *freopen(const char *restrict file, const char *restrict mode,
             FILE *restrict fp);
         FILE *_freopen_r(struct _reent *ptr, const char *restrict file,
             const char *restrict mode, FILE *restrict fp);

Description
Use this variant of `fopen` if you wish to specify a particular file descriptor fp (notably `stdin`, `stdout`, or `stderr`) for the file.

If fp was associated with another file or stream, `freopen` closes that other file or stream (but ignores any errors while closing it).

file and mode are used just as in `fopen`.

If file is `NULL`, the underlying stream is modified rather than closed. The file cannot be given a more permissive access mode (for example, a mode of "w" will fail on a read-only file descriptor), but can change status such as append or binary mode. If modification is not possible, failure occurs.

Returns
If successful, the result is the same as the argument fp. If the file cannot be opened as specified, the result is `NULL`.

Portability
ANSI C requires `freopen`.

Supporting OS subroutines required: `close`, `fstat`, `isatty`, `lseek`, `open`, `read`, `sbrk`, `write`.