Definition | #include <stdio.h> FILE *fopen(const char *f_name, const char *mode);
There is no functional difference between To process files > 2 GB, proceed as follows:
| |||||||||||||||||||||||||||||||||
Parameters | const char *f_name String specifying the file to be opened. f_name can be:
const char *mode String specifying the desired access mode. Optionally further functions may be controlled by additional specifications:
Access modes:
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. "...,tabexp=no" The tab character is not expanded. 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 When an existing file is opened and read, the LBP is always taken into account independently of the lbp switch:
"...,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. "...,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))):
The function behaves as if
The function behaves as if 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"
"...,split=no" When reading, records of maximum length are not concatenated with the following record. 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. When a non-existent file is created it is assigned the following file attributes by default:
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 Position of the read/write pointer in append mode: 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: 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:
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.
If If 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:
| |||||||||||||||||||||||||||||||||
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 |