Please enable JavaScript to view this site.

ESL Documentation

FILEIO Subroutine

Read the next line from a file into a variable.

call ReadNext(FILE_ID_IV, TARGET_SV)

FILE_ID_IV

A file identifier previously declared with the integer statement. This must be the same as the file identifier used when the file was opened.

TARGET_SV

A string variable to receive the data from the appropriate input line of the file. The target string will not contain any newline characters (\n) or carriage returns (\r).

Description

This subroutine allows you to read the next line of an ASCII or EBCDIC file into a string variable.

All input subroutines keep track of where the next line of the file begins, so you can call ReadNext( ) at any time, provided there are still lines in the file to be read.

All input subroutines also use an internal buffer to read from files. ReadNext( ) fills a buffer starting at the beginning of the next line, and expects to find the end of the line in the buffer. If it does not, it keeps increasing the buffer's size until it either finds the end of the line or runs out of system memory. A buffer size of 1K is best, and we recommend increments of 1K when increasing or decreasing the buffer's size.

Errors

FIO_E_BADLINENO

Specified line number not in file.

FIO_E_BUFFTOOSMALL

Internal buffer too small. Although an attempt was made to increase the buffer's size to fit in a complete line, system memory ran out before the current line could be accommodated. Make sure lines are terminated by a carriage return (\r) or newline (\n), otherwise the entire file will be interpreted as one line.

FIO_E_EOF

Attempt to read at end-of-file. All lines have been read.

FIO_E_FILEHANDLE

Invalid file handle - internal error.

FIO_E_FILELOCKED

File locked by another program.

FIO_E_LINETOOBIG

Current line exceeded buffer size. Although an attempt was made to increase the buffer's size to fit in a complete line, system memory ran out before the current line could be accommodated. Make sure lines are terminated by a carriage return (\r) or newline (\n), otherwise the entire file will be interpreted as one line.

FIO_E_NOTOPEN

File has not been opened. This error occurs when you try to read from a file that has not been opened.

FIO_E_OPENFORWRITE

Reading from file open for writing.

FIO_E_READACC

Read access violation. Should never happen during input.

FIO_E_SEEK

Error during seek - internal error. Contact your support representative.

Example

integer FileID

integer FileIDout

string  FileName is "input.txt"

string  FileNameOut is "output.txt"

string  Read is "read-only"

string  WriteTo is "Write"

string  Target

integer LineNum

string  NewLine is "\n"

record LineOut is

    LineNum 5

    Target 20

    NewLine 1

end record

include "fileio.inc"

 

# User types in line number desired, and the first 20

# characters of that line and the one after it are read

# in and written out.

# To be brief, error handling is omitted.

 

response to line from keyboard

    call OpenFile ( FileID, FileName, Read )

    call OpenFile ( FileIDout, FileNameOut, WriteTo )

    copy input to LineNum

    call ReadLineNumber ( FileID, LineNum, Target )

    call WriteRecord ( FileIDout, LineOut )

    call ReadNext ( FileID, Target )

    copy (1 + LineNum) to LineNum

    call WriteRecord ( FileIDout, LineOut )

    call CloseFile ( FileID )

    call CloseFile ( FileIDout )

See Also

ReadLineNumber( ) subroutine.