File and device input/output

Opening, creating and closing files

Other than the default standard input, output and error files, you must explicitly open files in order to read or write them. The two functions that do this are: open and creat (see open(2) and creat(2)). To read or write a file assumed to exist already, it must be opened by the following call:

fildes = open(name, oflag);

The argument name is a character string that represents a UNIX System V file system pathname. The oflag argument indicates whether the file is to be read, written, or ``updated'', that is, read and written simultaneously. The returned value fildes is a file-descriptor used to denote the file in subsequent calls that read, write or otherwise manipulate the file.

The function open resembles the function fopen in the Standard I/O Library, except that instead of returning a pointer to FILE, open returns a file-descriptor which is just an int (see fopen(3S) and Intro(3S)). Moreover, the values for the access mode argument oflag are different (the flags are found in /usr/include/fcntl.h):

The function open returns -1 if any error occurs; otherwise, it returns a valid open file-descriptor.

Trying to open a file that does not exist causes an error; hence, creat is used to create new files, or to re-write old ones. The creat system call creates the given file if it does not exist, or truncates it to zero length if it does exist; creat also opens the new file for writing and, like open, returns a file-descriptor. Calling creat as follows:

fildes = creat(name, pmode);

returns a file-descriptor if it created the file identified by the string name, and -1 if it did not. Trying to creat a file that already exists does not cause an error, but if the file already exists, creat truncates it to zero length.

If the file is brand new, creat creates it with the protection mode specified by the pmode argument. The UNIX System V file system associates nine bits of protection information with a file, controlling ``read'', ``write'' and ``execute'' permission for the ``owner'' of the file, for the owner's ``group'', and for any ``other'' users. Thus, a three-digit octal number specifies the permissions most conveniently. For example, 0755 specifies ``read'', ``write'' and ``execute'' permission for the ``owner'', and ``read'' and ``execute'' permission for the ``group'' and all ``other'' users.

A simplified version of the UNIX System V utility cp (a program which copies one file to another) illustrates this:

#define  NULL 0
#define  BUFSIZE 512
#define  PMODE 0644 /* RW owner, R group & others */

main(argc, argv) /* cp: copy fd1 to fd2 */ int argc; char *argv[ ]; { int fd1, fd2, n; char buf[BUFSIZE];

if (argc != 3) error("Usage: cp from to", NULL); if ((fd1 = open(argv[1], 0)) == -1) error("cp: can't open %s", argv[1]); if ((fd2 = creat(argv[2], PMODE)) == -1) error("cp: can't create %s", argv[2]);

while ((n = read(fd1, buf, BUFSIZE)) > 0) if (write(fd2, buf, n) != n) error("cp: write error", NULL);

exit(0); }

error(s1, s2) /* print error message and die */ char *s1, *s2; { printf(s1, s2); printf("\n");

exit(1); }

Simplified version of cp

The main simplification is that this version copies only one file, and does not permit the second argument to be a directory.

As stated earlier, there is a limit, OPEN_MAX, on the number of files which a process may have open simultaneously. Accordingly, any program which intends to process many files must be prepared to re-use file-descriptors. The function close breaks the connection between a file-descriptor and an open file, and frees the file-descriptor for use with some other file. Termination of a program via exit or return from the main program closes all open files.

Next topic: Random access -- lseek
Previous topic: Positioning and reading/writing files

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004