fopen

Opens a file and associates a stream with it.

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

Syntax

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

Parameters

filename

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

mode

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

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 the open operation fails, fopen 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 space to allocate a buffer.

6

EACCES

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

9

EINVAL

The mode argument is invalid.

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 in the calling process.

12

ENOSPC

The file system has insufficient space to create 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 fopen.

64

EISDIR

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

65

ENAMETOOLONG

The length of the filename argument 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 specified file resides on a read-only file system and mode requires write access.

81

EOVERFLOW

The named file is a regular file and the size of the file cannot be represented correctly in an object of type off_t.

Remarks

The fopen function opens the file whose name is the string pointed to by filename, and associates a stream with it.

Opening a file with read mode (r as the first character in the mode parameter) fails if the file does not exist or if it cannot be read. Opening a file with append mode (a as the first character in the mode parameter) causes all subsequent writes to the file to be forced to the current end-of-file, regardless of previous calls to the fseek function. When a file is opened with update mode (+ as the second or third character of the mode parameter), both input and output can be performed on the associated stream.

See Also