Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

fopen, fopen64 - Open a file

&pagelevel(4)&pagelevel

Definition

#include <stdio.h>

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

fopen and fopen64 open the file f_name and assign it a FILE structure and a file pointer.
The file pointer points to the FILE structure assigned.
The FILE structure is defined in the file <stdio.h>. It contains the required data for most of the functions in the standard I/O library.

There is no functional difference between fopen and fopen64, except that a large file identifier is stored in the file description that is linked to the file descriptor, i.e. the O_LARGEFILE bit is set. A file descriptor is returned that can be used to extend the file over 2 GB.

To process files > 2 GB, proceed as follows:

  • If the _FILE_OFFSET_BITS 64 define (see "Support for DMS and UFS files > 2 GB") is set, call fopen. fopen64 is then used implicitly with the appropriate parameters.

  • Otherwise, you have to call fopen64.


Parameters

const char *f_name

String specifying the file to be opened. f_name can be:

        • any valid BS2000 file name.
        • "link=linkname"
          linkname denotes a BS2000 link name.
        • "(SYSDTA)", "(SYSOUT)", "(SYSLST)"
          the corresponding system file.
        • "(SYSTERM)"
          terminal I/O.
        • "(INCORE)"
          temporary binary file that is created in virtual memory only.

const char *mode

String specifying the desired access mode. Optionally further functions may be controlled by additional specifications:

Additional specification

Function

tabexp=yes/no

Handling of the tab character (\t)

lbp=yes/no

Handling of the Last Byte Pointers (LBP)

split=yes/no

Processing text files with specification of a maximum record length

Access modes:

"r"

Open text file for reading. The file must already exist.

"w"

Open text file for writing. If the file exists, the old contents are deleted. If the
file does not exist, it is created.

"a"

Open text file for appending to the end of the file. If the file exists, it is
positioned to end of file, i.e. the old contents are preserved and the new data
is appended to the end of the file. If the file does not exist, it is created.

"rb"

Open binary file for reading. The file must already exist.

"wb"

Open binary file for writing. If the file exists, the old contents are deleted. If
the file does not exist, it is created.

"ab"

Open binary file for appending to the end of the file. If the file exists, it is
positioned to end of file, i.e. the old contents are preserved and the new data
is appended to the end of the file. If the file does not exist, it is created.

"r+w", "r+"

Open text file for reading and writing. The file must already exist. The old
contents are preserved.

"w+r", "w+"

Open text file for writing and reading. If the file exists, the old contents are
deleted. If the file does not exist, it is created.

"a+r", "a+"

Open text file for appending to the end of the file and for reading. If the file
exists, the old contents are preserved and the new data is appended to the
end of the file. An existing file is positioned differently depending on whether
KR or ANSI functionality is being used:
with KR functionality (applies to C/C++ versions prior to V3.0 only) to the end
of the file,
with ANSI functionality to the start of the file.
If the file does not exist, it is created.

"r+b", "rb+"

Open binary file for reading and writing. The file must already exist. The old
contents are preserved.

"w+b", "wb+"

Open binary file for writing and reading. If the file exists, the old contents are
deleted. If the file does not exist, it is created.

"a+b", "ab+"

Open binary file for appending to the end of the file and for reading. If the file
exists, the old contents are preserved and the new data is appended to the
end of the file. An existing file is positioned differently depending on whether
KR or ANSI functionality is being used:
with KR functionality (applies to C/C++ versions prior to V3.0 only) to the end
of the file,
with ANSI functionality to the start of the file.
If the file does not exist, it is created.

Tab character (\t)

In the mode parameter an optional entry controlling how the tab character (\t) is to be handled can be specified in addition to the access mode. This is relevant only for text files with the SAM and ISAM access methods.

"...,tabexp=yes"

The tab character is expanded into the appropriate number of blanks.
This is the default setting with KR functionality (applies to C/C++ versions prior to V3.0 only).

"...,tabexp=no"

The tab character is not expanded.
This is the default setting for ANSI functionality.

Last Byte Pointer (LBP)

In the mode parameter an optional entry controlling how the Last Byte Pointer (LBP) is to be handled can be specified in addition to the access mode. This is relevant only for binary files with PAM access mode. If lbp=yes is specified, a check is made to see whether LBP support is possible. If this is not the case, the fopen, fopen64 function will fail and errno is set to ENOSYS. The switch has further effects only when the file is closed.

When an existing file is opened and read, the LBP is always taken into account independently of the lbp switch:

        • If the file's LBP is not equal to 0, it is evaluated. Any marker which is present is ignored.

        • When LBP = 0, a marker is searched for, and the file length is determined from this. If no marker is found, the end of the last complete block is regarded as the end of file.

"...,lbp=yes"

When a file which has been modified or newly created is closed, no marker is written (even if one was present), and a valid LBP is set. In this way files with a marker can be converted to LBP without a marker.
In the case of NK files the last logical block is padded with binary zeros, in the case of K files the file is padded to the physical end of file.

"...,lbp=no"

When a file which has been newly created is closed, the LBP is set to zero (=invalid). A marker is written. In the case of NK files the last logical block is padded with binary zeros, in the case of K files the file is padded to the physical end of file.

When a file which has been modified is closed, the LBP is set to zero (=invalid). A marker is written only if a marker existed before. If no marker existed, none is written and the file ends with the complete last block. If the file had a valid LBP when it was opened, no marker is written as in this case it is assumed that no marker exists.In the case of NK files the last logical block is padded with binary zeros, in the case of K files the file is padded to the physical end of file.

If the lbp switch is not specified, the behavior depends on the environment variable LAST_BYTE_POINTER (see also “Environment variable LAST_BYTE_POINTER” (Last Byte Pointer (LBP))):

LAST_BYTE_POINTER=YES

The function behaves as if lbp=yes were specified.

LAST_BYTE_POINTER=NO

The function behaves as if lbp=no were specified.

Split/Nosplit switch

This switch controls the processing of text files with SAM access mode and variable record length when a maximum record length is also specified.

"...,split=yes"

        • The following applies when reading:
          If a record has the maximum record length, it is assumed that the following record is the continuation of this record and the records are concatenated.

        • The following applies when writing:
          A record which is longer than the maximum record length will be split into multiple records. If a record has precisely the maximum record length, a record of the length zero is written after it.

"...,split=no"

When reading, records of maximum length are not concatenated with the following record.
When reading with one of the functions fwrite, fprintf, printf, vfprintf, vprintf, fwprintf, wprintf, vfwprintf, vwprintf, fputs, fputws or puts, records which are longer than the maximum record length are truncated.

If the switch is not specified, "...,split=yes" applies.

Return val.

Pointer to the assigned FILE structure



if successful.


NULL pointer

if the file could not be opened, e.g. due to the absence of access permission, entry of an incorrect file name or link name etc.

Notes

The BS2000 file name or link name may be written in lowercase and uppercase letters. It is automatically converted to uppercase letters.

The inclusion of a "b" as the second or third character in the mode parameter causes the file to be opened as a binary file. This is relevant only for SAM files since only SAM files can be processed in both binary and text modes.
System files and ISAM files are always processed as text files. Specifying binary mode for these files leads to an error on opening.
(INCORE) and PAM files are always processed as binary files. For compatibility reasons files can be opened as binary files without explicit specification of the binary mode.

When a non-existent file is created it is assigned the following file attributes by default:


Binary file

Text file

Access method

SAM

SAM (KR functionality, applies to C/C++ versions prior to V3.0 only)

ISAM (ANSI functionality)

Record format

F

V

If a link name is used the following file attributes can be changed with the ADD-FILE-LINK command: access method, record length, record format, block length and block format. See also section “Cataloged disk files (SAM, ISAM, PAM)”.

Whenever the old contents of an already existing file are deleted (opened for writing or for writing and reading) the catalog attributes of this file are preserved.

When a file is opened for an update, reading and writing can be performed via the same file pointer. All the same, an output should not be immediately followed by an input without a preceding positioning operation (with fseek/fseek64, fsetpos/fsetpos64, rewind) or a fflush call. This also applies to an output that follows an input.

Position of the read/write pointer in append mode:
If you explicitly position the read/write pointer away from the end of a file that was opened in append mode (rewind, fsetpos/fsetpos64, fseek/fseek64), the way it is handled depends on whether you are using KR or ANSI functionality.
KR functionality (applies to C/C++ versions prior to V3.0 only): The current read/write pointer is ignored only when writing with the elementary function write and automatically positioned to the end of the file.
ANSI functionality: The current read/write pointer is ignored for all write functions and automatically positioned to the end of the file.

An attempt to open a non-existent file for reading ends with an error.

(INCORE) files can only be opened for writing ("w"), for writing and reading ("w+r") or for reading ("r"). Data must first be written. To be able to read in the written data, the following options are among those available:
If the file was opened only for writing, it can be opened for reading with the function freopen or freopen64. If it was opened for writing and reading, the read/write pointer can be set to the beginning of the file with rewind.

You may open a file for different access modes simultaneously, provided these modes are compatible with one another within the BS2000 data management system.

When a program begins, three file pointers - for standard input, standard output, and standard error output - are assigned to it automatically. The pointers are named as follows:

stdin

file pointer for standard input (terminal)

stdout

file pointer for standard output (terminal)

stderr

file pointer for standard error output (terminal)

A maximum of _NFILE files may be open simultaneously. _NFILE is defined as 2048 in <stdio.h>.

Record I/O

For opening files with record I/O the mode parameter has two additional options. These follow the access mode in the string (see above), each separated by a comma.

"...,type=record [,forg={seq/key}]"

type=record

The file is opened for record I/O.
If this option is omitted the file is opened for stream I/O.

forg=seq

The file is organized sequentially.
Sequential files may be SAM or PAM files.

forg=key

The file is organized indexed-sequentially.
Indexed-sequential files are ISAM files.

If forg is omitted, the file organization depends on the FCB type (FCBTYP) of the file:
The FCB type is defined by the catalog entry of an existing file or by a ADD-FILE-LINK command. Sequential organization is assumed for SAM and PAM files, and indexedsequential organization for ISAM files.

If forg is omitted and the FCB type is not defined (file does not exist, no ADD-FILE-LINK command), sequential file organization is assumed and a SAM file is created.

The following restrictions apply to record I/O. If these restrictions are ignored the file is not opened and an error return value is supplied:

  1. The file must be opened in binary mode ("b" specified in the access mode).

  2. "type=record" is permissible for SAM, PAM and ISAM files.

  3. "forg=seq" is permissible for SAM and PAM files, "forg=key" for ISAM files.

  4. The append mode (’a’) is not allowed with ISAM files. The position is determined by thekey. 

Example

/* program for copying from file1 and file2 to file3 */
#include <stdio.h>
#include <stdlib.h>
FILE *fp_1, *fp_2;
void copy(void);
int main(void)   /* file1 and file2 must exist */
{
   if((fp_1 = fopen("file1","r")) == NULL ||?? (fp_2 = fopen("file3","w"))
                       ==NULL)
      {
               /* program aborts, with return alue 1 */
        perror("fopen");
        exit(1);
      }
   copy();
               /* reassign file pointer from file1 to file2 */
   if((freopen("file2","r",fp_1)) == NULL)
               /* program aborts, with return value 2 */
     exit(2);
   copy();
   fclose(fp_1);
   fclose(fp_2);
   return 0;
}
void copy(void)
{
   int c;
   while((c = getc(fp_1)) != EOF)
        putc((char)c,fp_2);
}

See also

creat, creat64, fdopen, freopen, freopen64, ferror, open, open64, fclose, fseek, fseek64