Reopen a stream
#include <stdio.h>
FILE* freopen( const char* filename,
const char* mode,
FILE* fp );
FILE* freopen64( const char* filename,
const char* mode,
FILE* fp );
- filename
- The name of the file to open.
- mode
- The mode to use when opening the file.
For more information, see
fopen().
- fp
- The stream to associate with the file.
libc
Use the -l c option to
qcc
to link against this library.
This library is usually included automatically.
The freopen() and freopen64() functions close the open stream fp,
open the file specified by filename, and associate its stream
with fp.
The largest value that can be represented correctly in an object of type
off_t is the offset maximum in the open
file description.
(QNX Neutrino extension) The following mode changes are permitted:
- w to a
- a to w
- r+ to r
- r+ to w
- r+ to a
- r+ to w+
- r+ to a+
- w+ to r
- w+ to w
- w+ to a
- w+ to r+
- w+ to w+
- w+ to a+
- a+ to r
- a+ to w
- a+ to a
- a+ to r+
- a+ to w+
A pointer to the newly opened stream, or NULL if an error
occurs
(errno is set).
- EACCES
- Search permission is denied on a component of the filename
prefix, or the file exists and the permissions specified by
mode are denied, or the file doesn't exist and write permission is denied
for the parent directory of the file to be created.
- EBADF
- The underlying file descriptor is invalid or doesn't support the
requested mode change.
- EBADFSYS
- While attempting to open the named file, either the file itself or a
component of the filename prefix was found to be corrupted.
A system failure — from which no automatic recovery is possible —
occurred while the file was being written to, or while the directory was
being updated. You'll need to invoke appropriate
systems-administration procedures to correct this situation before
proceeding.
- EBUSY
- File access was denied due to a conflicting open
(see sopen()).
- EINTR
- The freopen() operation was interrupted by a signal.
- EINVAL
- The value of the mode argument is not valid.
- EISDIR
- The named file is a directory, and the mode
argument specifies write-only or read/write access.
- ELOOP
- Too many levels of symbolic links or prefixes.
- EMFILE
- Too many file descriptors are currently in use by this process.
- ENAMETOOLONG
- The length of the filename string exceeds
PATH_MAX, or a pathname component is longer than
NAME_MAX.
- ENFILE
- Too many files are currently open in the system.
- ENOENT
- Either the named file or the filename prefix doesn't
exist, or the filename argument points to an empty string.
- ENOMEM
- There is no memory for FILE structure.
- ENOSPC
- The directory or filesystem that would contain the new file can't
be extended.
- ENOSYS
- The freopen() function isn't implemented for the
filesystem specified in filename.
- ENOTDIR
- A component of the filename prefix isn't a directory.
- ENXIO
- The media associated with the file has been removed (e.g. CD, floppy).
- EOVERFLOW
- The named file is a regular file and the size of the file can't be
represented correctly in an object of type off_t.
- EROFS
- The named file resides on a read-only filesystem.
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
FILE* fp;
int c;
/* Reopen the stdin stream so it's reading
* from "file" instead of standard input.
*/
fp = freopen( "file", "r", stdin );
if( fp != NULL ) {
/* Now we can read from "file" using the
* stdin functions like fgetchar()...
*/
while( ( c = fgetchar() ) != EOF ) {
fputchar( c );
}
fclose( fp );
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
freopen() is ANSI,
POSIX 1003.1;
freopen64() is Large-file support
Safety: | |
Cancellation point |
Yes |
Interrupt handler |
No |
Signal handler |
No |
Thread |
Yes |
errno,
fclose(),
fcloseall(),
fdopen(),
fopen(),
fopen64()