Read_Block

See Also: File I/O Commands, Direct_Input, Read, Readln, Set_Channel_Position, Seqeof, Seqeol, Write, Channel command component, Sequential File I/O

Purpose

To read a block of data of a specified size from a sequential file, device, or text field and move it to a variable.

Syntax

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

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

Variable is required, may be of any simple data type or UChar[]. Additional variables may be on the command line, separated by spaces, without limit.

length

Length is required, if omitted, it defaults to zero. Can be -1 if used with a UChar[], this indicates that the remaining file should be read.

What It Does

Read_Block reads length characters into variable from the file or device named in the most recently executed Direct_Input command. Read_Block makes no distinctions among characters in the file (delimiters, embedders, spaces, tabs, discards), except the end of file character, which it uses to set Predefined Indicator SeqEOF True.

Read_Block is intended for use with non delimited files. Files delimited with commas and/or end of line characters can be read in with the read and Readln commands. Undelimited files may be text files or any type of data files.

You must set length and the pattern of Read_Block commands 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 in variable.

Example

Read_Block sMainInfo 70

This reads a block of 70 characters into the variable called sMainInfo.

 

Working with UChar Arrays

Read_Block can receive the data in a UChar array.  This can be used in place of a string variable and has the advantage that you don't have to worry about maximum string size and you don't have to concern yourself with embedded zeros.

If the variable receiving the data is a UChar[], it will resize the array to fit the data and copy the data into the array. You can use SizeOfArray to determine how many bytes were copied.

Read_Block must be passed a length. When the data is loaded the UChar array, will be resized fit the data. If the length passed exceeds the data remaining in the file, the array size will be sized to the actual length. When using UChar arrays, you can pass the value -1 to indicate that the remaining file should be read. This is probably how this will be most commonly used.

This can be used with Set_Field_Value and Get_Field_Value, which have also been extended to support UChar arrays.

Procedure ReadFromPDFFile String sInputFile

    UChar[] PDFManual

 

    // read a sequential file

    Direct_Input Channel 5 ("Binary:"+sInputFile)

    If (SeqEOF) Begin

        Procedure_Return

    End

    // read entire file into UChar array

    Read_Block Channel 5 PDFFile -1

    Close_Input Channel 5

 

    // write this to my table

    Set_Field_Value Product.PdfManual (RefTable(Product)) to PDFFile

End_Procedure

Notes

 

String sFragment sEOLVal

 

Move (Character(10)) to sEOLVal

Repeat

    Read_Block sFragment 80

Until (pos(sEOLVal, sFragment) > 0)