Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP Pascal/HP-UX Programmer's Guide > Chapter 3 Input/Output

Direct Input/Output

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

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 “Characteristics of Direct I/O Procedures” summarizes the characteristics of the predefined direct I/O procedures. (The I/O procedures in Table 3-3 “Characteristics of Associate Procedure” also work on direct access files.)

Table 3-8 Characteristics of Direct I/O Procedures

Procedure

Readdir

Writedir

Seek

State that file must be in

Read-write

Assigns value of

Specified component

Specified variable

Not applicable

To

Specified variable

Specified component

Not applicable

Advances current position index

To component following specified component

To specified component

After call, buffer is undefined

No

Yes

 

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

Title not available (Direct Input/Output )

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 “Characteristics of Direct File Functions” summarizes the characteristics of the predefined direct file functions.

Table 3-9 Characteristics of Direct File Functions

Function

Lastpos

Maxpos

Eof

State that file must be in

Read-write

Returns

Position number of highest-numbered component that you can read (the last component ever written)

Position number of highest-numbered component that you can write

Returns true if current position index is after lastpos; false otherwise

 

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 “Characteristics of Sequential File Functions” and Table 3-9 “Characteristics of Direct File Functions”).

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.
Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.