fopen(), fopen64()

Open a file stream

Synopsis:

#include <stdio.h>

FILE * fopen( const char * filename,
              const char * mode );
          
FILE * fopen64( const char * filename,
                const char * mode );

Arguments:

filename
The name of the file that you want to open.
mode
The access mode; see below.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The fopen() function opens a file stream for the file specified by filename. The fopen64() function is a large-file support version of fopen().

Note: In QNX Neutrino 6.6 or later, 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 Classification in What's in a Function Description?

The mode string begins with one of the following sequences:

a
Append: create a new file or open the file for writing at its end.
a+
Append: open the file or create it for update, writing at end-of-file; use the default file translation.
r
Open the file for reading.
r+
Open the file for update (reading and/or writing); use the default file translation.
w
Create the file for writing, or truncate it to zero length.
w+
Create the file for update, or truncate it to zero length; use the default file translation.

You can add the letter b to the end of any of the above sequences to indicate that the file is (or must be) a binary file (this is an ANSI requirement for portability to systems that make a distinction between text and binary files, such as DOS). Under QNX Neutrino, there's no difference between text files and binary files.

(QNX Neutrino 7.0 or later) You can add x to the w and w+ specifiers to prevent the function from overwriting the file if it already exists.

Note: When you're using a stream in update mode, writing can't be followed by reading without an intervening call to fflush(), or to a file-positioning function (fseek(), fsetpos() or rewind()). Similarly, reading can't be followed by writing without an intervening call to a file-positioning function, unless the read resulted in end-of-file.

The largest value that can be represented correctly in an object of type off_t is the offset maximum in the open file description.

Adjusting the buffer size

By default, each stream gets a buffer of BUFSIZ characters. It's allocated when you first read from or write to the stream, and the stream I/O functions use it for their internal purposes. If you're reading and writing large amounts of data, you can improve performance by increasing the size of this internal buffer from the default specified by BUFSIZE:

Returns:

A pointer to a file stream for success, 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.
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 fopen() operation was interrupted by a signal.
EINVAL
The value of the mode argument isn't 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 isn't enough memory for the FILE structure.
ENOSPC
The directory or filesystem that would contain the new file can't be extended.
ENOSYS
The fopen() 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 (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;

    fp = fopen( "report.dat", "r" );
    if( fp != NULL ) {
        /* rest of code goes here */
        fclose( fp );
        
        return EXIT_SUCCESS;
    }

    return EXIT_FAILURE;
}

Classification:

fopen() is ANSI, POSIX 1003.1; fopen64() is Large-file support

Safety:  
Cancellation point Yes
Interrupt handler No
Signal handler No
Thread Yes