Sample Code for Read Block

The sample code below should be entered inside the Edit window's Command mode and not inside the File I/O dialog box.

Suppose you have various external data files that contain miscellaneous part data and the first few characters of each file designate what the file is for. You can use the File/Read_Block command to read only those first few characters before deciding to read in and process every line. Consider this code:

C3=COMMENT/INPUT,Please type the name of the

,file code to search for.

ASSIGN/BLOCKSIZE=LEN(C3.INPUT)

ASSIGN/FILECODE=C3.INPUT

DO/

C1=COMMENT/INPUT,Please type the full pathway,

,filename, and extension to the

,file you want to process.

,Type [Q] to quit.

IF/C1.INPUT=="Q" OR C1.INPUT=="q"

COMMENT/OPER,You've chosen to quit. Routine now ending.

GOTO/END

END_IF/

V1=FILE/EXISTS,C1.INPUT

IF/V1<>0

COMMENT/OPER,"Data file ["+C1.INPUT+"] exists. Click OK to continue."

FPTR=FILE/OPEN,C1.INPUT,READ

V2=FILE/READ_BLOCK,FPTR,BLOCKSIZE

FILE/CLOSE,FPTR

IF/V2<>FILECODE

COMMENT/OPER,"The file's code of ["+V2+"] doesn't match"

,"the FILECODE of ["+FILECODE+"]."

END_IF/

UNTIL/V2==FILECODE

COMMENT/OPER,"File ["+C1.INPUT+"] is a match."

,"The file's code of ["+V2+"] matches"

,"the FILECODE of ["+FILECODE+"]."

COMMENT/OPER,Routine then processes the file.

END_IF/

ELSE/

COMMENT/OPER,"Data file ["+C1.INPUT+"] doesn't exist. Please retry using an existing data file."

GOTO/END

END_ELSE/

END=LABEL/

ROUTINE/END

Code Explanation

Some of this code is similar to that explained in "Sample Code for Read Character" or in "Sample Code for Read Line".

Only explanations unique to this example are given here.

ASSIGN/BLOCKSIZE=LEN(C3.INPUT)
This line uses creates a user defined variable named BLOCKSIZE that contains an integer equal to the number of characters found in C3.INPUT. This will be used as the size of the block of characters to read in.

ASSIGN/FILECODE=C3.INPUT
This line creates the FILECODE variable and gives it the value of C3.INPUT.

C1=COMMENT/INPUT
This comment stores the full pathway entered by the user into the C1.INPUT variable.

V1=FILE/EXISTS,C1.INPUT
This line checks for the existence of the file name defined in the C1 comment.

DO/
This line begins a DO / UNTIL loop. It bounds the block of code that allows the user to specify a file to read from. It will continue looping until the text assigned to FILECODE variable matches the text read from the file.

V2=FILE/READ_BLOCK,FPTR,BLOCKSIZE
This line reads the amount of characters equal to the integer contained in the BLOCKSIZE variable. The text is then stored in V2 variable.

IF/V2FILECODE
This line begins an IF / END IF code block that tests to see if the text in the V2 variable matches the text stored in the FILECODE variable. If it does match, then the routine continues running. Otherwise it displays a message saying the two codes don't match.

UNTIL/V2==FILECODE
This line checks the condition of the DO / UNTIL loop to see if the text in the V2 variable matches the text in the FILECODE variable. If the statement evaluates to false, the DO loop runs again, allowing the user to choose a different file name. If the statement evaluates to true, then the loop exits and the routine displays a message saying it matches. PC-DMIS could then continue to read each line of data from the specified data file.