File Input and Output

The table below lists input and output routines.

Input and output procedures and functions

Procedure or function Description

Append

Opens an existing text file for appending.

AssignFile

Assigns the name of an external file to a file variable.

BlockRead

Reads one or more records from an untyped file.

BlockWrite

Writes one or more records into an untyped file.

ChDir

Changes the current directory.

CloseFile

Closes an open file.

Eof

Returns the end-of-file status of a file.

Eoln

Returns the end-of-line status of a text file.

Erase

Erases an external file.

FilePos

Returns the current file position of a typed or untyped file.

FileSize

Returns the current size of a file; not used for text files.

Flush

Flushes the buffer of an output text file.

GetDir

Returns the current directory of a specified drive.

IOResult

Returns an integer value that is the status of the last I/O function performed.

MkDir

Creates a subdirectory.

Read

Reads one or more values from a file into one or more variables.

Readln

Does what Read does and then skips to beginning of next line in the text file.

Rename

Renames an external file.

Reset

Opens an existing file.

Rewrite Creates and opens a new file.

RmDir

Removes an empty subdirectory.

Seek

Moves the current position of a typed or untyped file to a specified component. Not used

with text files.

SeekEof

Returns the end-of-file status of a text file.

SeekEoln

Returns the end-of-line status of a text file.

SetTextBuf

Assigns an I/O buffer to a text file.

Truncate

Truncates a typed or untyped file at the current file position.

Write

Writes one or more values to a file.

Writeln

Does the same as Write, and then writes an end-of-line marker to the text file.

A file variable is any variable whose type is a file type. There are three classes of file: typed, text, and untyped. The syntax for declaring file types is given in File types.

Before a file variable can be used, it must be associated with an external file through a call to the AssignFile procedure. An external file is typically a named disk file, but it can also be a device, such as the keyboard or the display. The external file stores the information written to the file or supplies the information read from the file.

Once the association with an external file is established, the file variable must be opened to prepare it for input or output. An existing file can be opened via the Reset procedure, and a new file can be created and opened via the Rewrite procedure. Text files opened with Reset are read-only and text files opened with Rewrite and Append are write-only. Typed files and untyped files always allow both reading and writing regardless of whether they were opened with Reset or Rewrite.

Every file is a linear sequence of components, each of which has the component type (or record type) of the file. The components are numbered starting with zero.

Files are normally accessed sequentially. That is, when a component is read using the standard procedure Read or written using the standard procedure Write, the current file position moves to the next numerically ordered file component. Typed files and untyped files can also be accessed randomly through the standard procedure Seek, which moves the current file position to a specified component. The standard functions FilePos and FileSize can be used to determine the current file position and the current file size.

When a program completes processing a file, the file must be closed using the standard procedure CloseFile. After a file is closed, its associated external file is updated. The file variable can then be associated with another external file.

By default, all calls to standard I/O procedures and functions are automatically checked for errors, and if an error occurs an exception is raised (or the program is terminated if exception handling is not enabled). This automatic checking can be turned on and off using the {$I+} and {$I} compiler directives. When I/O checking is off, that is, when a procedure or function call is compiled in the {$I} state an I/O error doesn't cause an exception to be raised; to check the result of an I/O operation, you must call the standard function IOResult instead.

You must call the IOResult function to clear an error, even if you aren't interested in the error. If you don't clear an error and {$I+} is the current state, the next I/O function call will fail with the lingering IOResult error.

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment