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 );

Since:

BlackBerry 10.0.0

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() and fopen64() functions open a file stream for the file specified by filename. The fopen64() function is a 64-bit version of fopen(). 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 BlackBerry 10 OS, there's no difference between text files and binary files.

  • Opening a file in read mode (r in the mode) fails if the file doesn't exist or can't be read.
  • Opening a file in append mode (a in the mode) 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 (+ in the mode), both input and output may be performed on the associated stream.

When 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 shall be established as the offset maximum in the open file description.

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 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 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 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.

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

Last modified: 2014-06-24



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus