FREOPEN(3C) | Standard C Library Functions | FREOPEN(3C) |
freopen - open a stream
#include <stdio.h> FILE *freopen(const char *filename, const char *mode, FILE *stream);
The freopen() function first attempts to flush the stream and close any file descriptor associated with stream. Failure to flush or close the file successfully is ignored. The error and end-of-file indicators for the stream are cleared.
The freopen() function opens the file whose pathname is the string pointed to by filename and associates the stream pointed to by stream with it. The mode argument is used just as in fopen(3C).
If filename is a null pointer and the application conforms to SUSv3 (see standards(7)), the freopen() function attempts to change the mode of the stream to that specified by mode, as though the name of the file currently associated with the stream had been used. This behavior is not supported by streams that are backed by memory, such as fmemopen(3C). The following changes of mode are permitted, depending upon the access mode of the file descriptor underlying the stream:
If the filename is a null pointer and the application does not conform to SUSv3, freopen() returns a null pointer.
The original stream is closed regardless of whether the subsequent open succeeds.
After a successful call to the freopen() function, the orientation of the stream is cleared, the encoding rule is cleared, and the associated mbstate_t object is set to describe an initial conversion state.
The largest value that can be represented correctly in an object of type off_t will be established as the offset maximum in the open file description.
Upon successful completion, freopen() returns the value of stream. Otherwise, a null pointer is returned and errno is set to indicate the error.
The freopen() function will fail if:
EACCES
EBADF
EFAULT
EINTR
EISDIR
ELOOP
EMFILE
ENAMETOOLONG
ENFILE
ENOENT
ENOSPC
ENOTDIR
ENXIO
EOVERFLOW
EROFS
The freopen() function may fail if:
EINVAL
ENAMETOOLONG
ENOMEM
ENXIO
ETXTBSY
The freopen() function is typically used to attach the preopened streams associated with stdin, stdout and stderr to other files. By default stderr is unbuffered, but the use of freopen() will cause it to become buffered or line-buffered.
The freopen() function has a transitional interface for 64-bit file offsets. See lf64(7).
See attributes(7) for descriptions of the following attributes:
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
Interface Stability | Standard |
MT-Level | MT-Safe |
fclose(3C), fdopen(3C), fopen(3C), stdio(3C), attributes(7), lf64(7), standards(7)
March 25, 2020 | OmniOS |