freopen(), freopen64()
QNX SDP8.0C Library ReferenceAPIDeveloper
Reopen a stream
Synopsis:
#include <stdio.h>
FILE* freopen( const char* filename,
const char* mode,
FILE* fp );
FILE* freopen64( const char* filename,
const char* mode,
FILE* fp );
Arguments:
- filename
- The name of the file to open, or NULL if you want to reopen the file associated with fp.
- mode
- The mode to use when opening the file. For more information, see fopen().
- fp
- The stream to associate with the file.
Library:
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
Description:
The freopen() and freopen64() functions reopen a stream:
- If filename isn't NULL, these functions close the open stream fp, open the file specified by filename, and then associate its stream with fp.
- If filename is NULL, these functions reopen the file associated with the stream fp.
The freopen64() function is a large-file support version of freopen().
Note:
The large-file support functions and data types appear in the name space only if you define
_LARGEFILE64_SOURCE when you compile your code. For more information, see
Classificationin What's in a Function Description?
If an error occurs, freopen() or freopen64() will close the stream 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 OS extension) The following mode changes are permitted:
w
toa
a
tow
r+
tor
r+
tow
r+
toa
r+
tow+
r+
toa+
w+
tor
w+
tow
w+
toa
w+
tor+
w+
tow+
w+
toa+
a+
tor
a+
tow
a+
toa
a+
tor+
a+
tow+
Note:
If you're reading and writing large amounts of data, you can improve performance by increasing the size
of the internal buffer that's used for stream I/O.
For more information, see
Adjusting the buffer sizein the entry for fopen().
Returns:
A pointer to the newly opened stream, or NULL if an error occurs (errno is set).
Errors:
- 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
- All file descriptors available to the process are currently open.
- 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 underlying the path specified in filename.
- ENOTDIR
- A component of the filename prefix isn't a directory.
- ENXIO
- The media associated with the file (e.g., a CD) has been removed.
- 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.
Examples:
#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" instead of stdin. */
while( ( c = getchar() ) != EOF )
{
putchar(c);
}
fclose( fp );
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
Classification:
freopen() is ANSI, POSIX 1003.1; freopen64() is Large-file support
Safety: | |
---|---|
Cancellation point | Yes |
Signal handler | No |
Thread | Yes |
Page updated: