Definition | #include <stdio.h> int open(const char *f_name, int mode);
There is no functional difference between To process files > 2 GB, proceed as follows:
| |||||||
Parameters | const char *f_name String specifying the name of the file to be opened. f_name may be:
int mode Constant defined in the <stdio.h> header which specifies the desired access mode (or the corresponding octal value), namely:
Open for reading. The file must already exist.
Open for writing. The file must already exist. The previous contents are retained.
Open for writing. If the file exists, the previous contents are deleted. If the file does not exist, it is created.
Open for reading and writing. The file must already exist. The previous contents are retained.
Open for reading and writing. If the file exists, the previous contents are deleted. If the file does not exist, it is created.
Open for writing and reading. If the file exists, the previous contents are deleted. If the file does not exist, it is created.
Open for appending to the end of the file. The file must already exist. The file is positioned to end of file, i.e. the previous contents are preserved and the new text is appended to the end of the file.
Open for appending to the end of the file and for reading. The file must already exist. The old contents are preserved and the new text is appended to the end of the file. After it is opened, the file is positioned to the end of the file when KR functionality is being used (applies to C/C++ versions prior to V3.0 only), with ANSI functionality to the start of the file. lbp switch The lbp switch controls handling of the Last Byte Pointer (LBP). It is only relevant for binary files with PAM access mode and can be combined with all specifications permissible for When an existing file is opened and read, the LBP is always taken into account independently of the lbp switch:
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.
When a file which has been modified or 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 or newly created is closed, the LBP is set to zero (=invalid). 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. If the lbp switch is specified in both variants (O_LBP and O_NOLBP), the 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 O_LBP were specified.
The function behaves as if O_NOLBP were specified. 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. It can be combined with any of the other constants.
When reading with If the switch is not specified, the following applies:
| |||||||
Record I/O | The constant O_RECORD can be specified in the modus parameter to open files with record-oriented input/output (record I/O). It can always be combined with every other constant except O_LBP. Only in the case of ISAM files is adding to the end of the file not permitted, i.e. the combination with
This switch functions as follows:
| |||||||
Return val. | File descriptor | positive number that is used later to identify the file in elementary access operations ( | ||||||
-1 | if the file could not be opened, e.g. due to the absence of access authorization, entry of an invalid file name or link name etc. | |||||||
Notes | The BS2000 file name or link name can be written in both uppercase and lowercase. It is automatically converted to uppercase. If a non-existent file is created, the following applies by default: By using a link name 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 “System files (SYSDTA, SYSOUT, SYSLST)”. Whenever the old contents of an already existing file are deleted (0003, 01001), the catalog attributes of this file are preserved. Position of the read/write pointer in append mode: An attempt to open a non-existent file in the read (0000, 0002), update (0001), or append (0401, 0402) mode results in an error. You may open a file for different access modes simultaneously, provided these modes are compatible with one another within the BS2000 data management system. (INCORE) files can be only opened for writing (01001) or for writing and reading (0003). When a program starts, the standard files for input, output, and error output are automatically opened with the following file descriptors:
A maximum of _NFILE files may be open simultaneously. _NFILE is defined as 2048 in <stdio.h>. | |||||||
Example | The following program opens the file joke twice (for reading) and processes it with different file descriptors (fd1, fd2). #include <stdio.h> int fd1,fd2; char c; int n; int main(void) { /* open file "joke" for the first time for reading */ if((fd1=open("joke",0)) == -1) /* error in the first open */ printf("Error1\n"); /* open file "joke" for the second time for reading */ if((fd2=open("joke",0)) == -1) /* error in the second open */ printf("Error2\n"); /* reading is performed via fd1 until the first 'a' */ while((n=read(fd1,&c,1)) > 0 && (c != 'a')) /* output the read in character on standard output */ write(1,&c,n); /* reading is now performed via fd2 from the beginning of the file!! to the end of the file */ while((n=read(fd2,&c,1)) > 0) /* output the read in character on standard output */ write(1,&c,n); /* reading continues via fd1 following the first 'a', until the end of the file */ while((n=read(fd1,&c,1)) > 0) /* output the read in character on standard output */ write(1,&c,n); return 0; } | |||||||
See also | creat, creat64, fdopen, read, write, close |