2.2 UNIX I/O and Standard I/O

UNIX I/O functions are UNIX system services, now standardized by ISO POSIX-1 (the ISO Portable Operating System Interface).

UNIX I/O functions use file descriptors to access files. A file descriptor is an integer that identifies the file. A file descriptor is declared in the following way, where file_ desc is the name of the file descriptor:

int  file_desc;

UNIX I/O functions, such as creat, associate the file descriptor with a file. Consider the following example:

file_desc1 = creat("INFILE.DAT", 0, "rat=cr", "rfm=var");

This statement creates the file, INFILE.DAT, with file access mode 0, carriage-return control, variable-length records, and it associates the variable file_desc1 with the file. When the file is accessed for other operations, such as reading or writing, the file descriptor is used to refer to the file. For example:

write(file_desc1, buffer, sizeof(buffer));

This statement writes the contents of the buffer to INFILE.DAT.

There may be circumstances when you should use UNIX I/O functions and macros instead of the Standard I/O functions and macros. For a detailed discussion of both forms of I/O and how they manipulate the RMS file formats, see Chapter 1.

Standard I/O functions are specified by the ANSI C Standard.

Standard I/O functions add buffering to the features of UNIX I/O and use file pointers to access files. A file pointer is an object of type FILE *, which is a typedef defined in the <stdio.h> header file as follows:

typedef  struct  _iobuf  *FILE;

The _iobuf identifier is also defined in <stdio.h>.

To declare a file pointer, use the following:

FILE  *file_ptr;

You use the Standard I/O fopen function to create or open an existing file. For example:

#include <stdio.h>

main()
{
   FILE  *outfile;
   outfile  =  fopen("DISKFILE.DAT", "w+");
      .
      .
      .
}

Here, the file DISKFILE.DAT is opened for write-update access.

The DEC C RTL provides the following functions for converting between file descriptors and file pointers:


Previous Page | Next Page | Table of Contents | Index