Read

See Also: File I/O Commands, Direct_Input, Read_Block, Readln, Set_Channel_Position, SeqEOF, SeqEOL, Write, Channel command component, Sequential File I/O

Purpose

To read "words" of data from a sequential file, device, or text field and move them to one or more variables.

Syntax

Read [Channel {channel-num}] {variable} […{variable}]

channel channelNum

Input channel to read data from. If omitted, the channel will be that specified in the most recently executed Direct_Input, read, read_block, or readln command in which channel was specified or implied.

Variable

The first variable is required, may be of any type except Indicator and cannot be a constant. Additional variables may be on the command line, separated by spaces, without limit.

What It Does

The Read command reads data from the input file or device named in the most recently executed Direct_Input command. The first read command in the program reads data from the first line of the input file or device until it finds a comma or end of line character. Then it moves the data to variable. If the end character was a comma and there is another variable, data will be read into the next variable until the next comma or end of line.

Example

For the following examples, assume the file infile.txt has two lines, as follows:

 One, Dos , "Trois", "Vier ", 'Cinque'

 Six, Siete , "Huit", "Neun ", 'Dieci'

String s1 s2 s3 s4 s5 s6

Number n1 n2 n3 n4 n5 n6

Direct_Input "infile.txt"

Read s1 s2

The read command would read One into String Variable s1, and Dos (followed by two spaces), into Variable s2. The leading space before Dos would be discarded.

When the last variable has been read into, execution proceeds to the next command, even if unread data remains on the current line. If an end of line is encountered before the variables are exhausted, the remaining variables are made blank if they are of type String or zero if they are of type Date or Number, and execution proceeds to the next command.

Successive read commands pick up where the previous read command left off, until an end of line character is encountered. To read past an end of line character in a data file, you must use a readln command. When an end of line character is encountered, Predefined Indicator seqeol is set true. When an end of file character is encountered, Predefined Indicator seqeof is set true.

Assume the read command in the program above were followed by this command:

Read s3 s4 s5 s6

This command would read Trois into s3, Vier (followed by a space), into s4, and Cinqu into s5. The embedding characters (" and ') would be discarded. s6 would be blank, even if it had data in it previously, because an end of line character comes after Cinque. Any further read commands would read nothing until a readln command was executed.

The point in a file at which reading is done can also be changed by use of the set_channel_position command.

Read uses commas to divide sequential data into variables, and it discards leading spaces and tabs unless they are embedded in quotation marks. It ignores commas embedded between single or double quotation marks as field separators and instead treats them as part of the data. The comma is referred to as the "delimiter" character since it separates the variables on the input line. The quotation marks are referred as "embedding" characters. Embedding characters and delimiters are discarded from read data.

Read checks read data for the types of the destination variables, and truncates any items which exceed the declared length of String variables. Different types of variables may be intermixed in the same read command.

Assume the first read command above were replaced by a command which referred to the Numbers declared in the program:

Read n1 n2 n3

This command would generate three DataFlex errors, since the data does not qualify for Type Number.

Notes

 

'The Hungry "i"', "Johnny's Bar"

 

string sKeep1 sKeep2 sDiscard

read sDiscard sDiscard sDiscard sKeep1 sDiscard sKeep2

 

Repeat

    Read sVariable 

    Send ProcessVariable sVariable 

    If (SeqEOL) Readln

Until (SeqEOF)