1.6 Input and Output on OpenVMS Systems

After you learn how to link with the DEC C RTL and call DEC C functions and macros, you can use the DEC C RTL for its primary purpose: input/output (I/O).

Since every system has different methods of I/O, familiarize yourself with the OpenVMS-specific methods of file access. In this way, you will be equipped to predict functional differences when porting your source program from one operating system to another.

Figure 1-2 shows the I/O methods available with the DEC C RTL. The OpenVMS system services communicate directly with the OpenVMS operating system, so they are closest to the operating system. The OpenVMS Record Management Services (RMS) functions use the system services, which manipulate the operating system. The DEC C Standard I/O and UNIX I/O functions and macros use the RMS functions. Since the DEC C RTL Standard I/O and UNIX I/O functions and macros must go through several layers of function calls before the system is manipulated, they are furthest from the operating system.

Figure 1-2 I/O Interface from C Programs

The C programming language was developed on the UNIX operating system, and the Standard I/O functions were designed to provide a convenient method of I/O that would be powerful enough to be efficient for most applications, and also be portable so that the functions could be used on any system running C language compilers.

The DEC C RTL adds functionality to this original specification. Since, as implemented in the DEC C RTL, the Standard I/O functions recognize line terminators, the DEC C RTL Standard I/O functions are particularly useful for text manipulation. The DEC C RTL also implements some of the Standard I/O functions as preprocessor defined macros.

In a similar manner, the UNIX I/O functions originally were designed to provide a more direct access to the UNIX operating systems. These functions were meant to use a numeric file descriptor to represent a file. A UNIX system represents all peripheral devices as files to provide a uniform method of access.

The DEC C RTL adds functionality to the original specification. The UNIX I/O functions, as implemented in DEC C, are particularly useful for manipulating binary data. The DEC C RTL also implements some of the UNIX I/O functions as preprocessor defined macros.

The DEC C RTL includes the Standard I/O functions that should exist on all C compilers, and also the UNIX I/O functions to maintain compatibility with as many other implementations of C as possible. However, both Standard I/O and UNIX I/O use RMS to access files. To understand how the Standard I/O and UNIX I/O functions manipulate RMS formatted files, learn the fundamentals of RMS. See Section 1.6.1 for more information about Standard I/O and UNIX I/O in relationship to RMS files. For an introduction to RMS, see the Guide to OpenVMS File Applications.

Before deciding which method is appropriate for you, first ask this question: Are you concerned with UNIX compatibility or with developing code that will run solely under the OpenVMS operating system?

If you are writing system-level software, you may need to access the OpenVMS operating system directly through calls to system services. For example, you may need to access a user-written device driver directly through the Queue I/O Request System Service ($QIO). To do this, use the OpenVMS level of I/O; this level is recommended if you are an experienced OpenVMS programmer. For examples of programs that call OpenVMS system services, see the DEC C User's Guide for OpenVMS Systems.

You may never use the RMS or the OpenVMS system services. The Standard I/O and UNIX I/O functions are efficient enough for a large number of applications. Figure 1-3 shows the dependency of the Standard I/O and the UNIX I/O functions on RMS, and the various methods of I/O available to you.

Figure 1-3 Mapping Standard I/O and UNIX I/O to RMS

1.6.1 RMS Record and File Formats

To understand the capabilities and the restrictions of the Standard I/O and UNIX I/O functions and macros, you need to understand VAX Record Management Services (RMS).

RMS supports the following file organizations:

Sequential files have consecutive records with no empty records in between; relative files have fixed-length cells that may or may not contain a record; and indexed files have records that contain data, carriage-control information, and keys that permit various orders of access.

The DEC C RTL functions can access only sequential files. If you wish to use the other file organizations, you must use the RMS functions. For more information about the RMS functions, see the DEC C User's Guide for OpenVMS Systems.

RMS is not concerned with the contents of records, but it is concerned about the record format, which is the way a record physically appears on the recording surface of the storage medium.

RMS supports the following record formats:

You can specify a fixed-length record format at the time of file creation. This means that all records occupy the same amount of space in the file. You cannot change the record format once you create the file.

The length of records in variable length, VFC, and stream file formats can vary up to a maximum size that must be specified when you create the file. With variable-length record or VFC format files, the size of the record is held in a header section at the beginning of the data record. With stream files, RMS terminates the records when it encounters a specific character, such as a carriage- control or line-feed character. Stream files are useful for storing text.

RMS allows you to specify carriage-control attributes for records in a file. Such attributes include the implied carriage-return or the FORTRAN formatted records. RMS interprets these carriage controls when the file is output to a terminal, a line printer, or other device. The carriage-control information is not stored in the data records.

By default, files inherit the RMS record format, maximum record size and record attributes, from the previous version of the file, if one exists; to an OpenVMS system programmer, the inherited attributes are known as fab$b_rfm, fab$w_mrs and fab$b_rat. If no previous versions exist, the newly created file defaults to stream format with line-feed record separator and implied carriage-return attributes. (This manual refers to this type of file as a stream file.) You can manipulate stream files using the Standard I/O and the UNIX I/O functions of the DEC C RTL. When using these files and fixed-record files with no carriage control, there is no restriction on the ability to seek to any random byte of the file using the fseek or the lseek functions. However, if the file has one of the other RMS record formats, such as variable-length record format, then these functions, due to RMS restrictions, can seek only to record boundaries. Use the default VAX stream format unless you need to create or access files to be used with other VAX languages or utilities.

1.6.2 Access to RMS Files

RMS sequential files can be opened in record mode or stream mode. By default, STREAM_LF files are opened in stream mode; all other file types are opened in record mode. When opening a file, you can override these defaults by specifying the optional argument "ctx=rec" to force record mode, or "ctx=stm" to force stream mode. RMS relative and indexed files are always opened in record mode. The access mode determines the behavior of various I/O functions in the DEC C RTL.

One of the file types defined by RMS is an "RMS-11 stream" format file, corresponding to a value of FAB$C_STM for the record format. The definition of this format is such that the RMS record operation SYS$GET removes leading null bytes from each record. Because this file type is processed in record mode by the DEC C RTL, it is unsuitable as a file format for binary data unless it is explicitly opened with "ctx=stm", in which case the raw bytes of data from the file are returned.

1.6.2.1 Accessing RMS Files in Stream Mode

Stream access to RMS files is done with the block I/O facilities of RMS. Stream input is performed from RMS files by passing each byte of the on-disk representation of the file to your program. Stream output to RMS files is done by passing each byte from your program to the file. The DEC C RTL performs no special processing on the data.

When opening a file in stream mode, the DEC C RTL allocates a large internal buffer area. Data is read from the file using a single read into the buffer area and then passing the data to your program as needed. Data is written to the file when the internal buffer is full or when the fflush function is called.

1.6.2.2 Accessing RMS Record Files in Record Mode

Record access to record files is done with the record I/O facilities of RMS. The DEC C RTL emulates a byte stream by translating carriage-control characters during the process of reading and writing records. Random access is allowed to all record files, but positioning (with fseek and lseek) must be on a record boundary for VFC files, variable record files, or files with non-null carriage control. Positioning a record file causes all buffered input to be discarded and buffered output to be written to the file.

Record input from RMS record files is emulated by the DEC C RTL in two steps:

  1. The DEC C RTL reads a logical record from the file.

    If the record format is variable length with fixed control (RFM = VFC), and the record attributes are not print carriage control (RAT is not PRN), then the DEC C RTL concatenates the fixed-control area to the beginning of the record.

  2. The DEC C RTL expands the record to simulate a stream of bytes by translating the record's carriage-control information (if any).

In RMS terms, the DEC C RTL translates the record's carriage-control information using one of the following methods:

As you read from the file, the DEC C RTL delivers a stream of bytes resulting from the translations. Information that is not read from an expanded record by one function call is delivered on the next input function call.

The DEC C RTL performs record output to RMS record files in two steps.

The first part of the record output emulation is the formation of a logical record. As you write bytes to a record file, the emulator examines the information being written for record boundaries. The handling of information in the byte stream depends on the attributes of the destination file or device, as follows:

The second part of record output emulation is to write the logical record formed during the first step. The DEC C RTL forms the output record as follows:

1.6.2.2.1 Accessing Variable-Length or VFC Record Files in Record Mode

When you access a variable-length or VFC record file in record mode, many I/O functions behave differently than they would if they were being used with stream mode. This section describes these differences.

In general, the new-line character (\n) is the record separator for all record modes. On output, when a new-line character is encountered, a record is generated unless you specify an optional argument (such as "ctx=bin" or "ctx=xplct") that affects the interpretation of new lines.

The read and decc$record_read functions always read at most one record. The write and decc$record_write functions always generate at least one record.

decc$record_read and decc$record_write are equivalent, respectively, to read and write, except that they work with file pointers rather than file descriptors.

Unlike the read function which reads at most one record, the fread function can span records. Rather than read number_items records (where number_items is the third parameter to fread), fread tries to read the number of bytes equal to number_ items x size_of_item (where size_of_item is the second parameter to fread). The value returned by fread is equal to the number of bytes read divided by size_of_item.

However, the fwrite function always generates at least number_ items records.

The fgets and gets functions read to either a new-line character or a record boundary.

The fflush function always generates a record if there is unwritten data in the buffer. The same is true of close, fclose, fseek, lseek, rewind, and fsetpos, all of which perform implicit fflush functions.

A record is also generated whenever an attempt is made to write more characters than allowed by the maximum record size.

For more information on these functions, see the Reference Section.

1.6.2.2.2 Accessing Fixed-Length Record Files in Record Mode

When accessing a fixed-length record file in record mode, the I/O functions generally behave as described in Section 1.6.2.2.1.

The write, fwrite, and decc$record_write functions will fail if given a record size that is not an integral multiple of the maximum record size, unless the file was opened with the "ctx=xplct" optional argument specified. All other output functions will generate records at every nth byte, where n is the maximum record size.

If a new record is forced by fflush, the data in the buffer is padded to the maximum record size with null characters.


Note
This padding can cause problems for programs that seek to the end-of-file. For example, if a program were to append data to a file, then seek backwards in the file (causing an fflush to occur), and then seek to the end-of-file again, a zero-filled "hole" will have been created between the previous end-of-file and the new end-of-file if the previous end- of-file was not on a record boundary.

1.6.2.3 Example-Difference Between Stream Mode and Record Mode

Example 1-1 demonstrates the difference between stream mode and record mode access.

Example 1-1 Differences Between Stream Mode and Record Mode

/*
**  This program demonstrates the difference between record mode
**  and stream mode.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void process_records( const char *fspec, FILE *fp );

main()
{
    FILE *fp;

    fp = fopen("example-fixed.dat", "w", "rfm=fix", "mrs=40", "rat=none");
    if (fp == NULL) { perror("example-fixed"); exit(1); }
    printf("Record mode\n");
    process_records( "example-fixed.dat", fp );
    fclose( fp );

    printf("\nStream mode\n");
    fp = fopen( "example-streamlf.dat", "w" );
    if (fp == NULL) { perror("example-streamlf"); exit(1); }
    process_records( "example-streamlf.dat", fp );
    fclose( fp );
}

void process_records( const char *fspec, FILE *fp )
{
    int i, sts;

    char buffer[40];

    /*
    **  Write records of all 1's, all 2's and all 3's
    */
    for(i = 0; i < 3; i++ ) {
        memset( buffer, '1' + i, 40 );
        sts = fwrite (buffer, 40, 1, fp);
        if (sts != 1) { perror("fwrite"); exit(1); }
        }

    /*
    **  Rewind the file and write 10 characters of A's, then 10 B's,
    ** then 10 C's.
    **
    ** For stream mode, each fwrite call outputs 10 characters
    ** and advances the file position 10 characters.
    **
    ** For record mode, each fwrite merges the 10 characters
    ** into the existing 40-character record, updates the record and
    ** advances the file position 40 characters to the next record.
    */
    rewind (fp);
    for(i = 0; i < 3; i++ ) {
        memset(buffer, 'A' + i, 10 );
        sts = fwrite (buffer, 10, 1, fp);
        if (sts != 1) { perror("fwrite2"); exit(1); }
        }

    /*
    **  Now reopen the file and output the records.
    */

    fclose(fp);
    fp = fopen( fspec, "r");
    for(i = 0; i < 3; i++ ) {
        sts = fread (buffer, 40, 1, fp);
        if (sts != 1) perror("fread");
        printf("%.40s\n", buffer );
        }

    return;
}

Running this program produces the following output:

Record Mode
AAAAAAAAAA111111111111111111111111111111
BBBBBBBBBB222222222222222222222222222222
CCCCCCCCCC333333333333333333333333333333

Stream mode
AAAAAAAAAABBBBBBBBBBCCCCCCCCCC1111111111
2222222222222222222222222222222222222222
3333333333333333333333333333333333333333


Previous Page | Next Page | Table of Contents | Index