freopen

Opens a file and associates a stream with it.

Library:LibC
Classification:ANSI
Service:File and Directory I/O

Syntax

  #include <stdio.h> 
   
  FILE *freopen (
     const char   *filename,   
     const char   *mode,   
     FILE         *fp);
  

Parameters

filename

(IN) Points to the pathname of the file to be opened.

mode

(IN) Points to a string specifying the file open mode. See Standard File Open Modes.

fp

(IN) Points to the stream.

Return Values

If successful, returns a pointer to the object controlling the stream. This pointer must be passed as a parameter to subsequent functions for performing operations on the file.

If unsuccessful, freopen returns a NULL pointer and sets errno to one of the following:

Decimal

Constant

Description

1

ENOENT

A component of filename does not name an existing file, or filename is an empty string.

5

ENOMEM

Insufficient storage space is available

6

EACCES

Permissions required by the mode are denied to the file or a component of the path.

9

EINVAL

The value of the mode argument is not valid.

10

ENFILE

The maximum allowable number of files is currently open in the system.

11

EMFILE

The maximum number of file descriptors are currently open by the calling process.

12

ENOSPC

No free space remains in the file system for the file.

25

ENXIO

The named file is a character special or block special file, and the device associated with this special file does not exist.

63

EINTR

A signal interrupted the open operation.

64

EISDIR

The named file is a directory and mode requires write access.

65

ENAMETOOLONG

The length of the filename parameter exceeds the maximum path length, or a pathname component is longer than the maximum name length.

67

ENOTDIR

A component of the path is not a directory.

76

EROFS

The named file resides on a read-only file system and mode requires write access.

81

EOVERFLOW

The size of the file cannot be represented correctly in an object of type off_t.

Remarks

The freopen function flushes the stream and any file descriptor associated with the stream. It then closes the stream, opens the file, and associates the specified stream with it.

See Also