HP 3000 Manuals

Direct Input/Output [ HP Pascal/iX Programmer's Guide ] MPE/iX 5.0 Documentation


HP Pascal/iX Programmer's Guide

Direct Input/Output 

Direct input/output is input/output that is performed with direct files;
that is, files whose current position indices can be manipulated directly
by the program.  Direct input and output come from read-write files
opened by the procedure open (they cannot be textfiles).  Your program
can use the same direct file for input and output.

Table 3-8  summarizes the characteristics of the predefined direct I/O
procedures.  (The I/O procedures in Table 3-3  also work on direct
access files.)

          Table 3-8.  Characteristics of Direct I/O Procedures 

-------------------------------------------------------------------------------------------------
|                       |                       |                       |                       |
| Procedure             | Readdir               | Writedir              | Seek                  |
|                       |                       |                       |                       |
-------------------------------------------------------------------------------------------------
|                       |                                                                       |
| State that            |                              Read-write                               |
| file must             |                                                                       |
| be in                 |                                                                       |
|                       |                                                                       |
-------------------------------------------------------------------------------------------------
|                       |                       |                       |                       |
| Assigns               | Specified             | Specified             | Not applicable        |
| value of              | component             | variable              |                       |
|                       |                       |                       |                       |
-------------------------------------------------------------------------------------------------
|                       |                       |                       |                       |
| To                    | Specified             | Specified             | Not applicable        |
|                       | variable              | component             |                       |
|                       |                       |                       |                       |
-------------------------------------------------------------------------------------------------
|                       |                                               |                       |
| Advances              | To component following specified component    | To specified          |
| current               |                                               | component             |
| position              |                                               |                       |
| index                 |                                               |                       |
|                       |                                               |                       |
-------------------------------------------------------------------------------------------------
|                       |                       |                                               |
| After call,           |          No           |                      Yes                      |
| buffer is             |                       |                                               |
| undefined             |                       |                                               |
|                       |                       |                                               |
-------------------------------------------------------------------------------------------------

The procedures readdir, writedir, seek, read, and write have this
relationship:

This                      Is equivalent to this 

readdir(f,i,x);           seek(f,i);
                          read(f,x);

writedir(f,i,x);          seek(f,i);
                          write(f,x);

Example 1 

     PROGRAM prog;
     TYPE
        dirfile = FILE OF integer;
     VAR
                  f : dirfile;
        i1,i2,i3,i4 : integer;
     BEGIN
        open(f);           {Opens f for direct input/output}

        {READ TWO SPECIFIC COMPONENTS USING readdir AND read}
        readdir(f,50,i1);  {Puts the current position index at component 50.
                            Assigns component 50 to i1.
                            Advances the current position index.
                            Component 51 becomes the current component.}
        read(f,i2);        {Assigns component 51 to i2.}

        {READ TWO SPECIFIC COMPONENTS USING seek AND read}
        seek(f,70);        {Puts the current position index at component 70.}
        read(f,i3);        {Assigns component 70 to i3.
                            Advances the current position index.
                            Component 71 becomes the current component.}
        read(f,i4);        {Assigns component 71 to i4.}

        {WRITE TWO SPECIFIC COMPONENTS USING writedir AND write}
        writedir(f,10,i1); {Puts the current position index at component 10.
                            Assigns i1 to component 10.
                            Advances the current position index.
                            Component 11 becomes the current component.}
        write(f,i2);       {Assigns i2 to component 11.}

        {WRITE TWO SPECIFIC COMPONENTS USING seek AND write}
        seek(f,30);        {Puts the current position index at component 30.}
        write(f,i3);       {Assigns i3 to component 30.
                            Advances the current position index.
                            Component 31 becomes the current component.}
        write(f,i4);       {Assigns i4 to component 31.}
     END.

All of the sequential I/O procedures work the same way on direct files;
that is, they treat them like sequential files.  If you use both
sequential and direct I/O procedures on a file, the following guidelines
apply:

   *   After the sequential input procedure read, any reference to the
       buffer--even an explicit assignment to the buffer such as f^ :=
       30--assigns the value of the next component to the buffer.

   *   Because the components of a direct file can be written in any
       order, your program can skip components when it writes to a file
       directly.  If your program reads the file sequentially later, the
       values of the skipped components are unpredictable.

   *   The file-opening procedure open and the direct I/O procedures seek
       and writedir leave the buffer undefined.  After calling one of
       these procedures, your program must call get, read, or readdir
       before referencing the buffer implicitly (with a sequential I/O
       procedure) or explicitly.

Table 3-9  summarizes the characteristics of the predefined direct
file functions.

          Table 3-9.  Characteristics of Direct File Functions 

----------------------------------------------------------------------------------------
|              |                       |                       |                       |
| Function     | Lastpos               | Maxpos                | Eof                   |
|              |                       |                       |                       |
----------------------------------------------------------------------------------------
|              |                                                                       |
| State that   |                              Read-write                               |
| file must be |                                                                       |
| in           |                                                                       |
|              |                                                                       |
----------------------------------------------------------------------------------------
|              |                       |                       |                       |
| Returns      | Position number of    | Position number of    | Returns true if       |
|              | highest-numbered      | highest-numbered      | current position      |
|              | component that you    | component that you    | index is after        |
|              | can read (the last    | can write             | lastpos; false        |
|              | component ever        |                       | otherwise             |
|              | written)              |                       |                       |
|              |                       |                       |                       |
----------------------------------------------------------------------------------------

All of the sequential file functions work the same way on direct files,
except for a subtle difference in the eof function (compare Table 3-5  
and Table 3-9 ).

Example 2 

     PROGRAM prog;

     TYPE
        cfile = FILE OF char;

     VAR
        f : cfile;
        c : char;

     BEGIN
        reset(f);                       {Opens file for sequential input.}
        WHILE not(eof(f)) DO read(f,c); {Reads until eof is true.}
        read(f,c);                      {ERROR -- cannot read when eof is true.
                                         This statement would abort the program.}

        open(f);                        {Opens file for direct input/output.}

        IF lastpos(f) < maxpos(f) THEN BEGIN
           seek(f,lastpos(f)+1);        {Puts current position index beyond
                                         last component, making eof true.}

           read(f,c);                   {ERROR -- cannot read beyond lastpos(f).}

           write(f,c);                  {Writes beyond last component.
                                         The component written becomes the last.}
        END;
     END.



MPE/iX 5.0 Documentation