Reading Data

Reading Data

There are four DataFlex commands for reading data from a sequential I/O file, they are: Read, Readln, Read_Block, and Read_Hex. The first three are detailed below. Refer to the Language Reference for more information about each of these commands.

Read

The Read command reads comma-delimited data from the input file starting from the first line of the input file until it finds a comma or end-of-line character. Then it moves the data to a variable. The syntax for the Read command is:

Read  [channel {channel-num.}]  {variable1}  [{variable2 .. variableN}]

Where:

If the channel specification is omitted then data is read from whichever channel was last referenced, or channel 0 if no channel has been referenced. Each Read command must specify at least one variable for receiving the read data.

Successive Read statements pick up at the end of the last data that was read, 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, the Boolean expression seqeol returns True. When an end-of-file character is encountered, the Boolean expression seqeof returns True. An example of using the Read command is:

Direct_Input  "c:\Names.txt"

 

Repeat

    Read  sFirstName

    Read  sLastName

    If (Seqeol) Readln

Until (seqeof)

 

Close_Input

You can specify more than one {variable} per Read command. This is equivalent to executing multiple Read commands, one per {variable}. The Repeat statement in the previous example could be rewritten as shown below:

Repeat

    Read  sFirstName  sLastName

    If (Seqeol) Readln

Until (seqeof)

 

Readln

The Readln command reads data from the input file starting from the first line of the input file it finds an end-of-line character. Then it moves the data to a variable.

The syntax for the Readln command is:

Readln  [channel {channel-num.}]  [{variable1} .. variableN}]

Where:

If the channel specification is omitted then data is read from whichever channel was last referenced, or channel 0 if no channel has been referenced. If no {variable} is specified in the Read command the data is read in, then discarded.

Successive Readln statements pick up at the end of the last data that was read. After each Readln the Boolean expression seqeol returns False. When an end-of-file character is encountered, the Boolean expression seqeof returns True. An example of using the Readln command is:

Direct_Input  "c:\Names.txt"

 

Repeat

    Readln  sWholeName

Until (seqeof)

 

Close_Input

You can specify more than one {variable} per Readln command. This is equivalent to executing multiple Read commands, one per {variable} followed by a Readln with no variable.

Read_Block

The Read_Block command reads a specified number of bytes of data from the input file. Then it moves the data to a variable. The syntax for the Read_Block command is:

Read_Block  [channel {channel-num.}]  {variable} {length}

Where:

If the channel specification is omitted then data is read from whichever channel was last referenced, or channel 0 if no channel has been referenced.

Successive Read_Block statements pick up at the end of the last data that was read. When an end-of-file character is encountered, the Boolean expression seqeof returns True. An example of using the Read_Block command is:

Direct_Input  "c:\PhotoID.bmp"

Read_Block  sPhoto  16000

Close_Input

The Read_Block command makes no distinctions among characters in the file (delimiters, spaces, tabs), except the end-of-file character, which delimits the end of the file. Read_Block is intended for use with non-delimited text or binary-data files.

You must set the length and the pattern of Read_Block statements according to your prior knowledge of the structure of the data in the input file, since it cannot be inferred from delimiting characters. The maximum length is governed by the maximum number of characters that can be stored in the receiving variable (refer to the section on String variables for further details).