0% found this document useful (0 votes)
7 views

26 - Using File Input - Output

This document provides an overview of file input/output (I/O) concepts and operations in measurement routines, including opening and closing files, reading and writing data, positioning the file pointer, and checking for the end of files or lines. The main topics covered are understanding basic file I/O concepts like file pointers and delimiters, using the file dialog box, opening and closing files, reading and writing characters, lines, blocks of text, and data up to a delimiter, positioning the file pointer, saving and recalling the file position, copying/moving/deleting files, and checking for file existence and the end of a file/line. Sample code is provided for each operation.

Uploaded by

sokaiya raman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

26 - Using File Input - Output

This document provides an overview of file input/output (I/O) concepts and operations in measurement routines, including opening and closing files, reading and writing data, positioning the file pointer, and checking for the end of files or lines. The main topics covered are understanding basic file I/O concepts like file pointers and delimiters, using the file dialog box, opening and closing files, reading and writing characters, lines, blocks of text, and data up to a delimiter, positioning the file pointer, saving and recalling the file position, copying/moving/deleting files, and checking for file existence and the end of a file/line. Sample code is provided for each operation.

Uploaded by

sokaiya raman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Table of Contents

Using File Input / Output.................................................................................................. 1

Using File Input / Output: Introduction ......................................................................... 1

Understanding Basic File I/O Concepts ....................................................................... 2

Using the File I/O Dialog Box....................................................................................... 3

Opening a File for Reading or Writing.......................................................................... 3

Sample Code for File Open...................................................................................... 4

Closing an Opened File after Reading or Writing ........................................................ 6

Sample Code for File Close ..................................................................................... 7

Reading a Character from a File .................................................................................. 7

Sample Code for Read Character ............................................................................ 8

Reading a Line from a File......................................................................................... 10

Sample Code for Read Line ................................................................................... 11

Sample Code Dealing with Numbers Containing Preceding Zeros ........................ 13

Reading a Block of Text from a File ........................................................................... 15

Sample Code for Read Block ................................................................................. 17

Reading Text up to a Delimiter .................................................................................. 19

Sample Code for Read Up To ................................................................................ 20

Writing a Character to a File ...................................................................................... 21

Sample Code for Write Character .......................................................................... 22

Writing a Line to a File ............................................................................................... 24

Sample Code for Write Line ................................................................................... 25

Writing a Block of Text to a File ................................................................................. 26

i
Table of Contents

Sample Code for Write Block ................................................................................. 28

Positioning a File Pointer at the Beginning of a File................................................... 29

Sample Code for Rewind to Start ........................................................................... 30

Saving a File Pointer's Current Position..................................................................... 31

Sample Code for Save File Position....................................................................... 32

Recalling a Saved File Pointer's Position................................................................... 33

Sample Code for Recall File Position ..................................................................... 34

Copying a File............................................................................................................ 35

Sample Code for File Copy .................................................................................... 36

Moving a File ............................................................................................................. 37

Sample Code for File Move.................................................................................... 38

Deleting a File............................................................................................................ 39

Sample Code for File Delete .................................................................................. 40

Checking for a File's Existence .................................................................................. 41

Sample Code for File Exists ................................................................................... 42

Displaying a File Dialog Box ...................................................................................... 43

Sample Code for File Dialog .................................................................................. 44

Checking for the End of a File or the End of a Line ................................................... 45

Sample code for EOF and EOL.............................................................................. 45

ii
Using File Input / Output
Using File Input / Output: Introduction
This chapter explains how to input and output information to and from your
measurement routines. The available menu options allow for opening files in read or
write mode. Data can then be read from or written to these files. The file I/O commands
allow data to be read in from external files that are to be used in a measurement routine
. Also, measurement and tolerancing information can be written back out to files using
these commands. You can also perform other file operations by using these commands.

This chapter details these file I/O operations and includes functional examples for each
of the various operations. These examples use items discussed in the "Branching by
Using Flow Control" chapter and the "Using Expressions and Variables" chapter.

The main topics described in this topic include:

• Understanding Basic File I/O Concepts


• Using the File I/O Dialog Box
• Opening a File for Reading or Writing
• Closing an Opened File after Reading or Writing
• Reading a Character from a File
• Reading a Line from a File
• Reading a Block of Text from a File
• Reading Text up to a Delimiter
• Writing a Character to a File
• Writing a Line to a File
• Writing a Block of Text to a File
• Positioning a File Pointer at the Beginning of a File
• Saving a File Pointer's Current Position
• Recalling a Saved File Pointer's Position
• Copying a File
• Moving a File
• Deleting a File
• Checking for a File's Existence
• Displaying a File Dialog Box
• Checking for the End of a File or the End of a Line

1
Using File Input / Output

Understanding Basic File I/O Concepts


Checking for File Existence:
For all the file I/O operations, you probably want to first check for the file's existence.
This should probably be put in an IF / THEN loop so that if the check fails, you can
notify the user. When you write to a file, you must first create the file inside the windows
environment.

See "Checking for a File's Existence".

Opening and Closing Files:


For operations that read from or write to files, you need to first open them to your
system's processes. You do this by assigning the file to a variable called a file pointer.
When you open a file, you can specify whether the file is opened for reading, writing
(overwriting), or appending. Once opened, you can then read from or write to the file.
When you're finished working with a file, you should close the file pointer; this closes the
file and allows it to be accessed by other system processes. You cannot open files that
are already opened by another process.

See "Opening a File for Reading or Writing" and "Closing an Opened File after Reading
or Writing".

File Pointers and Positions:


File pointers are variables that point to a file. They store an opened file's name and
location and are then used to read from or write to that file. Once a file is opened and
set to a file pointer, the pointer behaves like a cursor acts in a word processor. They
indicate where you are currently reading from or writing to within the file.

• If you're appending to a file, the file pointer is usually at the end of the file.
• If you're reading a file or overwriting a file, the file pointer should usually start at
the beginning of a file.

Using Delimiters When Writing or Reading

When writing data, consider using delimiters to separate pieces of data. This makes it
easier to read the data back into a measurement routine. A delimiter can be any
character or string of characters. For example, suppose you have a point, named PNT1
with the X,Y, and Z measured values of 2.5,4.3,6.1. You can easily write these values
separated by a comma delimiter into a data file with code similar to the following:

FILE/WRITELINE,FPTR,PNT1.X + "," + PNT1.Y + "," + PNT1.Z

When reading data, you can separate the incoming data based on a specified delimiter
and place the data into variables for later manipulation. For example, suppose you want
to read in the same X, Y, and Z values listed above. The values should be in a single
2
Using the File I/O Dialog Box

line of text like this: 2.5,4.3,6.1. You can separate the text at the comma and place
those values into corresponding variables using a line of code similar to the following:

V1=FILE/READLINE,FPTR,{ValX}+","+{ValY}+","+{ValZ}

You can then use ValX, ValY, and ValZ as normal variables in your measurement
routine. Resulting in: ValX = 2.5, ValY = 4.3, and ValZ = 6.1.

Using the File I/O Dialog Box


All file I/O commands are initially inserted into the measurement routine by selecting the
appropriate file I/O menu option (select Insert | File I/O Command from the menu).
Once a command exists in the Edit window, you can then press F9 on the command to
access its associated File I/O dialog box.

A sample File I/O dialog box

This dialog box provides a visual way to edit the current file I/O command. Alternately,
you can modify a command inside the Edit window by using the techniques discussed in
the "Using the Edit Window" chapter.

You should not use this dialog box to insert new file I/O commands. That must be done
by selecting the appropriate menu option or by typing the commands directly into the
Edit window.

Opening a File for Reading or Writing


The Insert | File I/O Command | File Open menu option allows you to put a command
in the Edit window that opens a disk file during execution of the measurement routine.

Files can be opened simply to view information or to add and save information.

3
Using File Input / Output

The syntax of this command in the Edit window is:

<filepointername> =File/Open,<filename>,<openmode>

Descriptions for some of the components of this command are:

<filepointername>
This is the user chosen ID of the filepointer that is used to access the opened file.
This ID is used to refer to the open file in other file I / O commands.

<filename>
This is the filename of the disk file to open.

<openmode>
This is the mode the file should be opened in. Files can be opened in the following
modes: Read, Write, or Append.

To access the dialog box associated with this File I / O command:

1. Open the Edit window.


2. Place your cursor on the File Open command.
3. Press F9.

Sample Code for File Open

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

This code opens a file named TEST.TXT for reading, writing, and appending. It stores
the file name to a file pointer named FPTR.

4
Opening a File for Reading or Writing

FPTR=FILE/OPEN,C:\PCDMISW\TEST.TXT,READ
FPTR=FILE/OPEN,C:\PCDMISW\TEST.TXT,WRITE
FPTR=FILE/OPEN,C:\PCDMISW\TEST.TXT,APPEND

5
Using File Input / Output

Be aware that you can use an input comment to take the full pathway as an input and
use it in a FILE/OPEN command. You can also do the same thing by using the
FILE/DIALOG command. Consider these examples:

C1=COMMENT/INPUT,Type the full pathway and name of the file.


V1=FILE/DIALOG, CHOOSE A FILE TO OPEN
FPTR=FILE/OPEN,C1.INPUT,READ
FPTR=FILE/OPEN,V1,READ

See "Displaying a File Dialog Box".

Closing an Opened File after Reading or


Writing
The Insert | File I/O Command | File Close menu option allows you to insert a
command in the Edit window that closes an opened file when the measurement routine
is executed. Closing files frees up resources used when files are open and commits any
changes that have been made to the file to disk.

The syntax of this command in the Edit window is:

File/Close, <filepointername>,<closemode>

Descriptions for some of the components of this command are:

<filepointername>
This is the ID used to identify the file and is created when the file is opened.

<closemode>
This parameter has two options, KEEP or DELETE. Using KEEP, PC-DMIS simply
closes the file defined in the file pointer. Using DELETE, PC-DMIS closes the file
and then deletes it.

To access the dialog box associated with this File I / O command:

1. Open the Edit window.


2. Place your cursor on the File Close command.
3. Press F9.

6
Reading a Character from a File

Sample Code for File Close

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

This code simply closes the file assigned to the file pointer, FPTR:

FILE/CLOSE,FPTR,KEEP

This code, which uses the DELETE parameter, closes and deletes the file assigned to
FPTR:

FILE/CLOSE,FPTR,DELETE

Reading a Character from a File


The Insert | File I/O Command | Reading Commands | Read Character menu option
places a command in the Edit window that reads a single character from the file
specified by the filepointername field (see syntax below). It assigns that character to the
variable specified in the variable name field.

The syntax of this command in the Edit window is:

<varname> = File/ReadCharacter,<filepointername>

Descriptions for some of the components of this command are:

7
Using File Input / Output

<filepointername>
This is the ID used to open the file.

<varname>
This is the name of the variable that holds that character.

To access the dialog box associated with this File I / O command:

1. Open the Edit window.


2. Place your cursor on the Read Character command.
3. Press F9.

Sample Code for Read Character

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

Consider this example that reads in a line from a data file one character at a time until it
encounters a space.

V1=FILE/EXISTS,test.txt
IF/V1<>0
COMMENT/OPER,Able to read from data file. Click OK to
continue.
ASSIGN/V3=""
FPTR=FILE/OPEN,D:\Program Files\pcdmis35\test.txt,READ
DO/

8
Reading a Character from a File

V2=FILE/READ_CHARACTER,FPTR
ASSIGN/V3=V3+V2
UNTIL/V2==" "
FILE/CLOSE,FPTR
COMMENT/OPER,"The first word from a line of text from the
file is: "+V3
END_IF/
ELSE/
COMMENT/OPER,Wasn't able to read from data file. Routine
will now quit.
GOTO/END
END_ELSE/
END=LABEL/
ROUTINE/END

Code Explanation

V1=FILE/EXISTS
This line checks to see if the specified file exists. The file must be placed in the directory
where PC-DMIS resides for this code to work, otherwise the line containing the file must
also contain the full pathway for the file. V1 receives the result of the file check. It's a
non-zero value if it exists; 0 otherwise.

IF/V1<>0
This line takes the value of V1 and checks to see if it evaluates to a non-zero value. If
so, then a comment appears signifying that it's ready to begin the read process. If equal
to zero then the measurement routine ends.

ASSIGN/V3=""
This line creates an empty string and assigns it to V3. The code uses this variable to
build a string from the individual read in characters. If you don't create the empty string,
then V3 has its default value of 0.

FPTR=FILE/OPEN
This line opens the specified file for reading and assigns it to the default file pointer
FPTR.

DO
This line begins a DO / UNTIL loop. It bounds the FILE/READ_CHARACTER code so
that characters are continually read in one at a time. The loop exits whenever it reads in
a character space.

9
Using File Input / Output

V2=FILE/READ_CHARACTER,FPTR
This line reads in a character from the open file tied to the file pointer, FPTR. The
character is stored in the variable, V2.

ASSIGN/V3=V3+V2
This line uses the empty V3 variable, concatenates the string V3 with V2, and then
reassigns the value to V3. So, with subsequent runs of the DO/UNTIL loop, V3 will get
one more character added to it.

UNTIL/V2==" "
This line ends the DO / UNTIL loop once the FILE/READ_CHARACTER code encounters
a space character from the opened file.

FILE/CLOSE,FPTR
This line closes the opened data file, thereby allowing it to be accessed by other system
processes. The rest of the code finishes running and displays the first word from the
data file in an operator comment.

Reading a Line from a File


The Insert | File I/O Command | Reading Commands | Read Line menu option
places a command in the Edit window that reads a line from the specified file during
execution. This command sets the variable specified by the variable ID to 1 (true) or 0
(false) to indicate success (true) or failure (false) of the call. The expression required by
this command can be used to delimit the line read in and to automatically fill up
variables and references with data read in from the file. Information is read in from the
input file up to the next carriage return character.

The syntax of this command in the Edit window is:

<varname> = File/ReadLine,<filepointername>,<expr>

Descriptions for some of the components of this command are:

<varname>
This is the name of the variable that holds the result indicating success or failure
of the ReadLine command. It returns "OK" or "EOF".

<filepointername>
This is the name specified for the file pointer when the file was opened.

<expr>
This is the destination variable(s) for the input data. Input data can be delimited by

10
Reading a Line from a File

text to allow for ease in parsing incoming lines of data. Variables and feature
references should be surrounded by curly brackets.

To access the dialog box associated with this File I / O command:

1. Open the Edit window.


2. Place your cursor on the Read Line command.
3. Press F9.

Sample Code for Read Line

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

Consider this example that reads in a line from a data file one line at a time until the
FILE/READ_LINE command encounters an empty line. The measurement routine then
displays the resulting block of text and quits.

V1=FILE/EXISTS,test.txt
IF/V1<>0
COMMENT/OPER,Able to read from data file. Click OK to
continue.
ASSIGN/V3=""
FPTR=FILE/OPEN,D:\Program Files\pcdmis35\test.txt,READ
DO/
V2=FILE/READLINE,FPTR,{LINE}

11
Using File Input / Output

ASSIGN/V3=V3+LINE
COMMENT/OPER,"The current value of variable V3 is:
,V3
UNTIL/V2=="EOF"
FILE/CLOSE,FPTR
COMMENT/OPER,"The block of text reads as follows: "
,V3
END_IF/
ELSE/
COMMENT/OPER,Wasn't able to read from data file. Routine
will now quit.
GOTO/END
END_ELSE/
END=LABEL/
ROUTINE/END

Code Explanation

Much of this code is similar to that explained in "Sample Code for Read Character".
Only code unique to this example is listed here.

DO
This line begins a DO / UNTIL loop. It bounds the FILE/READ_LINE code so that the
lines are continually read in one at a time. The loop exits when it reaches the end of the
file.

V2=FILE/READLINE,FPTR,{LINE}
This line reads in all the text until it encounters a carriage return. Instead of storing the
text in V2, like FILE/READ_CHARACTER would, however, this code acts differently.

• V2 in this case returns two values: either "OK" or "EOF". "OK" if there's still a line
to read in. "EOF" if the end of the file is reached.
• The {LINE} code is a user-entered variable that stores the actual text. It is
enclosed in curly brackets to tell PC-DMIS it's a variable and not a part of any
delimiting text. Without the curly brackets, PC-DMIS would look for a string of
characters in the file called "LINE" and would return only the text after "LINE" and
before the carriage return.

ASSIGN/V3=V3+LINE
This line uses the empty V3 variable and concatenates the string V3 with LINE, and

12
Reading a Line from a File

then reassigns the concatenated value to V3. So, with subsequent runs of the
DO/UNTIL loop, V3 will get one more line added to it.

UNTIL/V2=="EOF"
This line tests for the condition of the DO / UNTIL loop. Once the FILE/READLINE
code encounters the end of file, the loop exits. Once the routine flow exits the loop, the
rest of the code finishes running and displays the entire block of code inside an operator
comment.

Result=File/ReadLine,F1,"Part ID :"+{V1} - This causes any text


appearing in the read in line after the text "Part ID:" to be assigned to V1. The line is
read in from the file opened using F1 as the file pointer name. The result of the read
(success or failure) would be stored in the variable Result.

File/ReadLine,F1,"Location:"+{VARX}+","+{VARY}+","+{VARZ}+","+{V
ARI}+","+{VARJ}+","+{VARK}
ASSIGN/CIR1.XYZ=MPOINT(VARX,VARY,VARZ)
ASSIGN/CIR1.IJK=MPOINT(VARI,VARJ,VARK)

The above three command lines read in comma delimited text after the string
"Location:" and store the values in the X, Y, Z, and I, J, K values of CIR1.

File/ReadLine,F1,"Value # "+loopvar+" : "+{var2} - This causes var2 to


be filled with the text appearing after the colon. The loopvar variable in this example is
not surrounded by curly brackets and as a result contributes to the delimiting text.

Sample Code Dealing with Numbers Containing


Preceding Zeros
If the file you are reading contains lines of numbers, you will notice that PC-DMIS
ignores preceding zero characters. For example, if your line contained a value of
005450, it would read this value strictly as a number and return the value of 5450,
ignoring the two preceding zeros. You may or may not want this.

Suppose you have a text file created by an external barcode reader software and it
contains these two lines of data:

290291143;582750;0010

291143;5827;0010

13
Using File Input / Output

You could use some simple code like this to get the number values in-between the
semi-colons:

ASSIGN/FIRST_VALUE=0
ASSIGN/SECOND_VALUE=0
ASSIGN/THIRD_VALUE=0
ASSIGN/LINENUM=1
FPTR=FILE/OPEN,D:\TEMP\CODES.TXT,READ
DO/
INLINE=FILE/READLINE,FPTR,{FIRST_VALUE}+";"+{SECOND_VALUE}+"
;"+{THIRD_VALUE}
COMMENT/OPER,NO,"LINE NUMBER: "+LINENUM
,"First Value: "+FIRST_VALUE
,"Second Value: "+SECOND_VALUE
,"Third Value: "+THIRD_VALUE
UNTIL/INLINE=="EOF"
FILE/CLOSE,FPTR,KEEP

While this will successfully parse the lines of text and return the number values, it will
also remove any preceding zeros for any values it returns. So, the THIRD_VALUE
variable would contain a value of 10, instead of 0010.

To keep the preceding zero values, you would need to treat the whole line as a string
and instead use the INDEX, LEFT, and MID string functions to locate the positions of
semi-colons in a line of text and obtain the number values:

FPTR=FILE/OPEN,D:\TEMP\CODES.TXT,READ
ASSIGN/LINENUM=1
DO/
LINESTATUS=FILE/READLINE,FPTR,{LINESTR}
ASSIGN/LINESTR=STR(LINESTR)
ASSIGN/FIRST_INDEX=INDEX(LINESTR,";")
ASSIGN/FIRST_VALUE=STR(LEFT(LINESTR,FIRST_INDEX-1))
ASSIGN/REMAINSTR=STR(MID(LINESTR,(FIRST_INDEX)))
ASSIGN/SECOND_INDEX=INDEX(REMAINSTR,";")
ASSIGN/SECOND_VALUE=STR(LEFT(REMAINSTR,SECOND_INDEX-1))
ASSIGN/THIRD_VALUE=STR(MID(REMAINSTR,SECOND_INDEX))
COMMENT/OPER,NO,"LINE NUMBER: "+LINENUM

14
Reading a Block of Text from a File

,"First Value: "+FIRST_VALUE


,"Second Value: "+SECOND_VALUE
,"Third Value: "+THIRD_VALUE
ASSIGN/LINENUM=LINENUM+1
UNTIL/LINESTATUS=="EOF"
FILE/CLOSE,FPTR,KEEP

Explanation of Code

Much of this code is similar to what is explained above. Only code explanations unique
to the mentioned string functions are listed here.

ASSIGN/FIRST_INDEX=INDEX(LINESTR,";")
This line locates the position of the first semi-colon in the line and assigns that to the
FIRST_INDEX variable.

ASSIGN/FIRST_VALUE=STR(LEFT(LINESTR,FIRST_INDEX-1))
This line assigns the FIRST_VALUE variable the string of characters up to, but not
including, the first semi-colon in the LINESTR variable. LINESTR contains the entire line
of text.

ASSIGN/REMAINSTR=STR(MID(LINESTR,(FIRST_INDEX)))
This line assigns the REMAINSTR variable (standing for "remaining string") the string of
left over characters starting from the FIRST_INDEX position (the position of the first
semi-colon) until the end of the line.

ASSIGN/SECOND_INDEX=INDEX(REMAINSTR,";")
This searches inside the REMAINSTR variable for another semi-colon (the second semi
colon in the line) and assigns the position to the SECOND_INDEX variable.

ASSIGN/SECOND_VALUE=STR(LEFT(REMAINSTR,SECOND_INDEX-1))
This line assigns the SECOND_VALUE variable the string of characters up to, but not
including, the first semi colon in the REMAINSTR variable (the second semi-colon in the
entire line).

ASSIGN/THIRD_VALUE=STR(MID(REMAINSTR,SECOND_INDEX))
This line assigns the THIRD_VALUE variable the string of characters starting from the
SECOND_INDEX position, until the end of the line.

Reading a Block of Text from a File


The Insert | File I/O Command | Reading Commands | Read Block menu option
places a command in the Edit window that reads in a block of characters from an open
15
Using File Input / Output

file at execution time. The amount of characters read in is indicated by the size
parameter.

The syntax of this command in the Edit window is:

<varname>=File/Read_Block,<fptrname>,<size>

Descriptions for some of the components of this command are:

<varname>
This is a variable id for the variable that receives the value indicating success or
failure of the read block operation.

<fptrname>
This is the name specified for the file pointer when the file was opened.

<size>
This is the number of characters to read.

To access the dialog box associated with this File I / O command:

1. Open the Edit window.


2. Place your cursor on the Read Block command.
3. Press F9.

16
Reading a Block of Text from a File

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
17
Using File Input / Output

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.

18
Reading Text up to a Delimiter

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.

Reading Text up to a Delimiter


The Insert | File I/O Command | Reading Commands | Read Up To menu option
places a command in the Edit window that reads all text "up to" one of the given
delimiters from the specified file during execution. Any text read by this command is
placed in the specified destination variable. The command stops reading text when PC-
DMIS encounters the following:

• Defined delimiters
• Carriage returns
• Line feed characters

If the end of the file is reached, the destination variable is set to "EOF" (End of File).

The syntax of this command in the Edit window is:

<varname> = FILE/READ_UPTO,<fptrname>,<delimiters>

Descriptions for some of the components of this command are:

<varname>
This is the name of the destination variable.

<fptrname>
This is the name specified for the file pointer when the file was opened.

<delimiters>
This is a string which contains zero or more delimiter characters.

To access the dialog box associated with this File I / O command:

19
Using File Input / Output

1. Select the Edit Window to open the Edit window.


2. Place your cursor on the FILE/READ command.
3. Press F9. The File I/O dialog box opens.

Once the dialog box appears:

1. Type the variable name that receives the read in information into the Variable ID
box.
2. Type the file pointer name into the File Pointer ID box.
3. Type the delimiter into the Text box (be sure and use quotation marks around
your chosen delimiter).
4. Click OK.

Sample Code for Read Up To

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

Consider this example where you have a text file named "sample.txt" in your c:\temp
directory which contains this information on the first line.

root:x:0:0:root:/root:/bin/bash

To use the Read Up To command on this file:

1. Insert a FILE/OPEN command in the Edit window.

20
Writing a Character to a File

2. Use a File Pointer Name of your choice to name your File Open command. This
example uses "sample" as the file pointer name.

The File Open command should look something like this:

SAMPLE =FILE/OPEN,C:\TEMP\SAMPLE.TXT,READ

Now, use PC-DMIS Read Up To commands to define some variables that call different
segments of data. This example uses the following variables looking for a ":" (without
the quotation marks) as the delimiter.

USERNAME =FILE/READ_UPTO,SAMPLE,:
PASSWORD =FILE/READ_UPTO,SAMPLE,:
USER =FILE/READ_UPTO,SAMPLE,:

Thus, when PC-DMIS executes these lines, it sets

username = root

password = x

user = 0

To display this on screen during execution, you can use an operator comment such as
the one shown here:

COMMENT/OPER,The following text is read in from sample.txt


,Username:
,USERNAME
,Password:
,PASSWORD
,User:
,USER

Writing a Character to a File


The Insert | File I/O Command | Writing Commands | Write Character menu option
inserts a command into the Edit window that causes a single character to be output to a
disk file upon execution.

The syntax of this command in the Edit window is:

21
Using File Input / Output

File/Write_Character,<fptrname>,<expr>

Descriptions for some of the components of this command are:

<fptrname>
This is the name of the file pointer specified when the file was opened.

<expr>
This is the character to be written to file. If the expression evaluates to more than
one character, only the first character is written.

To access the dialog box associated with this File I / O command:

1. Open the Edit window.


2. Place your cursor on the Write Character command.
3. Press F9.

Sample Code for Write Character

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

Consider this code that writes a string provided by the user to a data file one character
at a time.

C1=COMMENT/INPUT,Type the name of the file to write


,to (include the complete path).
FPTR=FILE/OPEN,C1.INPUT,WRITE

22
Writing a Character to a File

C2=COMMENT/INPUT,Type something to send to the file.


,This will send the string one character
,at a time.
ASSIGN/COUNT=0
ASSIGN/LENGTH=LEN(C2.INPUT)
DO/
ASSIGN/WRITETHIS=MID(C2.INPUT,COUNT,1)
FILE/WRITE_CHARACTER,FPTR,WRITETHIS
ASSIGN/COUNT=COUNT + 1
UNTIL/COUNT==LENGTH

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.

FPTR=FILE/OPEN,C1.INPUT,WRITE
This line opens the file specified in the C1 comment for writing, and assigns it to the file
pointer, FPTR. All data in this file will get overwritten as long as the file pointer begins at
the start of the data file.

ASSIGN/COUNT=0
This line assigns a user defined variable COUNT a value of zero. This is used for
looping purposes to print the string one character at a time.

ASSIGN/LENGTH=LEN(C2.INPUT)
This line uses the LEN( ) function to return the length of a string. This function takes
one parameter, the string. It counts the number of characters in the string (including
spaces) and returns an integer value of that amount. In this case, the user defined
variable, LENGTH holds this value.

DO/
This line begins a DO / UNTIL loop. Code between the DO and the UNTIL statements
will be executed until the loop's condition evaluates to true.

ASSIGN/WRITETHIS=MID(C2.INPUT,COUNT,1)
This line creates a user defined variable called WRITETHIS and uses the MID( )
function to return a substring character from the C2.INPUT string and give it to
WRITETHIS.

MID( ) takes three parameters.

23
Using File Input / Output

• Parameter 1: is the string from which to get values. In this case C2.INPUT
is used.
• Parameter 2: is the position in the string to take the character from. The
first character in a string would be position 0, the second position 1, the
third position 2 and so forth. In this case, the variable COUNT is used.
• Parameter 3: is how many characters starting from the position of the
second parameter to grab. In this case, the value of 1 is used (the sample
only writes one character at a time, so there's no reason to get more).

FILE/WRITE_CHARACTER,FPTR,WRITETHIS
This line writes the character stored in the WRITETHIS variable to the file specified by
the file pointer, FPTR.

ASSIGN/COUNT=COUNT+1
This line takes the current COUNT value, increments it by one, and then places the new
value back into COUNT.

UNTIL/COUNT==LENGTH
This line tests the condition of the DO / UNTIL loop. In this case, the loop will keep
incrementing the COUNT variable until it has the same value as the LENGTH variable.
Then the loop will exit, ending the routine.

Writing a Line to a File


The Insert | File I/O Command | Writing Commands | Write Line menu option inserts
a command into the Edit window that causes a line of text to be output to a disk file
upon execution. Use expression syntax to output variables and measurement routine
information to file. A carriage return is automatically appended to the text written out.

The syntax of this command in the Edit window is:

File/WriteLine,<fptrname>,<expr>

Descriptions for some of the components of this command are:

<fptrname>
This is the name of the file reference specified when the file was opened.

<expr>
This is the text to be written to file. Expressions can be used in this field.

To access the dialog box associated with this File I / O command:

24
Writing a Line to a File

1. Open the Edit window.


2. Place your cursor on the Write Line command.
3. Press F9.

Sample Code for Write Line

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 want to export some measured XYZ values to a data file. The following
code allows you to input a feature label and a data file and send the X,Y, and Z data for
that feature to a data file.

C1=COMMENT/INPUT,Type the label of the feature.


,to use.
C2=COMMENT/INPUT,Type the name of the file to write
,to (include the complete path).
FPTR=FILE/OPEN,C2.INPUT,APPEND
ASSIGN/FEATNAME=C1.INPUT
ASSIGN/ALLVALS=FEATNAME.X+","+FEATNAME.Y+","+FEATNAME.Z
COMMENT/OPER,"Text to write is: "+ALLVALS
FILE/WRITELINE,FPTR,ALLVALS
FILE/CLOSE,FPTR

25
Using File Input / Output

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.

FPTR=FILE/OPEN,C2.INPUT,APPEND
This line opens the file specified in the C2 comment for appending, and assigns it to the
file pointer, FPTR. If instead, you change APPEND to WRITE, then existing content in the
data file will get overwritten.

ASSIGN/FEATNAME=C1.INPUT
This line assigns the string of the feature label from C1.INPUT to the user defined
variable, FEATNAME.

ASSIGN/ALLVALS=FEATNAME.X+","+FEATNAME.Y+","+ FEATNAME.Z
This line gives the user defined variable ALLVALS the value of
FEATNAME.X,FEATNAME.Y,FEATNAME.Z, in other words it now holds the X, Y, and Z
values of the feature label typed into the C1 input comment.

FILE/WRITELINE,FPTR,ALLVALS
This line writes the values contained in ALLVALS to the file specified by the file pointer,
FPTR.

Writing a Block of Text to a File


The Insert | File I/O Command | Writing Commands | Write Block menu option
inserts a command into the Edit window that causes a block of text to be output to a
disk file upon execution. Use expression syntax to output the variables and
measurement routine information to file. Unlike the write line command, write block does
not append a carriage return at the end.

The syntax of this command in the Edit window is:

File/WriteBlock,<fptrname>,<expr>

Descriptions for some of the components of this command are:

<fptrname>
This is the name of the file reference specified when the file was opened.

<expr>
This is the text to be written to file. Expressions can be used in this field.

26
Writing a Block of Text to a File

Unlike the write line command, write block does not append a carriage return
at the end. However, if you need to place text on a new line inside your text block, you
can insert a carriage return and line feed manually by using the CHR(10) code outside
of your quoted string, as shown here in this example:

FILE/WRITEBLOCK,FPTR, "CHR(10) inserts text... " + CHR(10) + "


...on a new line."

This would yield this result inside your output file:

CHR(10) inserts text...

...on a new line.

Notice that if CHR(10) is inside the quotation marks the actual text of CHR(10) gets sent
to the file.

To access the dialog box associated with this File I / O command:

1. Open the Edit window.


2. Place your cursor on the Write Block command.
3. Press F9.

27
Using File Input / Output

Sample Code for Write Block

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

The following code writes whatever the user inputs into an input comment, and appends
a colon to be used as a delimiter.

C1=COMMENT/INPUT,Type any string. PC-DMIS will append a


colon (for delimiter purposes) and write the string to a
file of your choice
C2=COMMENT/INPUT,Type the name of the file to write
,to (include the complete path).
FPTR=FILE/OPEN,C2.INPUT,APPEND
ASSIGN/WRITETHIS=C1.INPUT+":"
COMMENT/OPER,"Text to write is: "+WRITETHIS
FILE/WRITELINE,FPTR,WRITETHIS
FILE/CLOSE,FPTR

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.

FPTR=FILE/OPEN,C2.INPUT,APPEND
This line opens the file specified in the C2 comment for appending, and assigns it to the
file pointer, FPTR.

ASSIGN/WRITETHIS=C1.INPUT+":"
This line appends a colon to the text contained in C1.INPUT and assigns the new string
to the user defined variable, WRITETHIS.

FILE/WRITELINE,FPTR,WRITETHIS
This line writes the values contained in WRITETHIS to the file specified by the file
pointer, FPTR. You can later read-in text from the file by using the colon as a delimiter.

28
Positioning a File Pointer at the Beginning of a File

Positioning a File Pointer at the


Beginning of a File
The Insert | File I/O Command | Position Commands | Rewind To Start menu option
inserts a command into the Edit window that will position the file pointer to the beginning
of the file stream.

The syntax of this command in the Edit window is:

File/Rewind,<fptrname>

Descriptions for some of the components of this command are:

<fptrname>
This is the name of the file pointer to reposition at the beginning of the file.

To access the dialog box associated with this File I / O command:

1. Open the Edit window.


2. Place your cursor on the Rewind to Start command.
3. Press F9.

29
Using File Input / Output

Sample Code for Rewind to Start

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

Consider this example that reads in data from an external file one line at a time. After
each line, you have the option of starting over and reading from the beginning of the file.
This illustrates the use of the FILE/REWIND command.

C1=COMMENT/INPUT,Please type a file to read from.


,(include the full path)
V1=FILE/EXISTS,C1.INPUT
IF/V1<>0
DO/
FPTR=FILE/OPEN,C1.INPUT,READ
C2=COMMENT/YESNO,Do you want to read from the beginning?
IF/C2.INPUT == "YES"
FILE/REWIND,FPTR
END_IF/
V2=FILE/READLINE,FPTR,{LINE}
COMMENT/OPER,"The current line is: " + LINE
UNTIL/V2=="EOF"
END_IF/
FILE/CLOSE,FPTR
COMMENT/OPER,Routine quitting.

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.

C2=COMMENT/YESNO
This line asks if you want to start reading the file from the beginning. It stores the
YES/NO response into the variable, C2.INPUT.

30
Saving a File Pointer's Current Position

IF/C2.INPUT == "YES"
This line begins an IF / END IF block. It tests the condition of C2.INPUT having the
value of YES. If the condition is true, then PC-DMIS executes the lines following the IF
statement. If the condition is false, then PC-DMIS executes the code following the
END_IF statement.

FILE/REWIND,FPTR
This line rewinds the file pointer to the beginning of the data file.

END_IF/
This line quits the IF / END IF code block.

Saving a File Pointer's Current Position


The Insert | File I/O Command | Position Commands | Save File Position menu
option inserts a command into the Edit window that saves the current position of a file
pointer within the file stream. The saved position can later be recalled using the recall
file position command.

The syntax of this command in the Edit window is:

File/SavePosition,<fptrname>

Descriptions for some of the components of this command are:

<fptrname>
This is the name of the file pointer whose file position is saved.

To access the dialog box associated with this File I / O command:

1. Open the Edit window.


2. Place your cursor on the Save File Position command.
3. Press F9.

31
Using File Input / Output

Sample Code for Save File Position

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

Consider this example that reads in data from an external file one line at a time. After
each line, you have the option of saving the file position for later recall. This illustrates
the use of the FILE/SAVE_POSITION command.

C1=COMMENT/INPUT,Please type a file to read from.


,(include the full path)
V1=FILE/EXISTS,C1.INPUT
IF/V1<>0
DO/
FPTR=FILE/OPEN,C1.INPUT,READ
C2=COMMENT/YESNO,Do you want to save the file position and
recall it later? The loop will quit.
IF/C2.INPUT == "YES"
FILE/SAVE_POSITION,FPTR
GOTO/QUITLOOP
END_IF/
V2=FILE/READLINE,FPTR,{LINE}
COMMENT/OPER,"The current line is: " + LINE
UNTIL/V2=="EOF"

32
Recalling a Saved File Pointer's Position

END_IF/
FILE/CLOSE,FPTR
QUITLOOP=LABEL/
COMMENT/OPER,You've stopped reading.
ROUTINE/END

Code Explanation

This code is similar to that explained in "Sample Code for Rewind to Start".

Only explanations unique to this example are given here.

C2=COMMENT/YESNO
This line asks if you want to store the current file position and exit the loop. It stores the
YES/NO response into the variable, C2.INPUT.

FILE/SAVE_POSITION,FPTR
This line stores the file pointer's position in the file stream.

As long as you open the same file with same file pointer name in the same
measurement routine, you can recall a stored file position and continue reading where
you left off. To continue this example, see the "Sample Code for Recall File Position"
topic.

Recalling a Saved File Pointer's Position


The Insert | File I/O Command | Position Commands | Recall File Position inserts a
command into the Edit window that recalls a previously-saved file position. Use the save
file position command to save a position within an open file.

The syntax of this command in the Edit window is:

File/RecallPosition, <fptrname>

Descriptions for some of the components of this command are:

<fptrname>
This is the name of the file pointer whose position is being recalled.

To access the dialog box associated with this File I / O command:

1. Open the Edit window.


2. Place your cursor on the Recall File Position command.

33
Using File Input / Output

3. Press F9.

Sample Code for Recall File Position

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

This example opens a previously-closed file, uses a previous file pointer, and recalls the
stored file pointer's saved position. It then reads in data from that position. This code
illustrates the use of the FILE/RECALL_POSITION command. It continues the code
sample given in the "Sample Code for Save File Position" topic.

COMMENT/OPER,The routine will now recall the stored file


position.
FPTR=FILE/OPEN,C1.INPUT,READ
FILE/REWIND,FPTR
COMMENT/OPER,To test, file has been rewound.
,The first line will be read in to test the rewind.
V3=FILE/READLINE,FPTR,{LINE}
COMMENT/OPER,The first line is:
,LINE
FILE/REWIND,FPTR
FILE/RECALL_POSITION,FPTR
COMMENT/OPER,Previously stored file position has been
recalled.

34
Copying a File

,Data on the line at the stored position will now print.


V4=FILE/READLINE,FPTR,{STORED}
COMMENT/OPER,The text at the stored position is:
,STORED

Code Explanation

This code is similar to that explained in "Sample Code for Rewind to Start".

Only explanations unique to this example are given here.

FILE/RECALL_POSITION,FPTR
This line recalls the stored file pointer position in the file stream for the file pointer
designated as FPTR.

V4=FILE/READLINE,FPTR,{STORED}
This line reads in the next line after the stored file pointer position and assigns it to the
user-defined variable of STORED. This variable is then printed out in the next operator
comment.

Copying a File
The Insert | File I/O Command | File Copy menu option inserts a command into the
Edit window that causes a file copy operation to occur upon execution.

The syntax of this command in the Edit window is:

File/Copy,<srcfilename>,<destfilename>,<replacemode>

Descriptions for some of the components of this command are:

<srcfilename>
This is the name of the source file (the file copied from).

<destfilename>
This is the name of the destination file (the file copied to).

<replacemode>
This is the action to take if the destination file already exists. The two modes are
overwritten and fail if the destination exists.

To access the dialog box associated with this File I / O command:

1. Open the Edit window.

35
Using File Input / Output

2. Place your cursor on the File Copy command.


3. Press F9.

Sample Code for File Copy

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

The following code asks for a file name to copy and for a destination directory and file to
copy to.

C1=COMMENT/INPUT,Please type a file you want to copy.


,(Include full file path)
C2=COMMENT/INPUT,Please type a destination file name.
,(Include full file path)
V1=FILE/EXISTS,C1.INPUT
IF/V1<>0
COMMENT/OPER,File exists to copy. Copying commencing.
FILE/COPY,C1.INPUT,C2.INPUT,FAIL_IF_DEST_EXISTS
V2=FILE/EXISTS,C2.INPUT
IF/V2==0
COMMENT/OPER,"File doesn't exist at: " + C2.INPUT
,Copy ending.
ROUTINE/END

36
Moving a File

END_IF/
ELSE/
COMMENT/OPER,File copy successful.
ROUTINE/END
END_ELSE/
END_IF/
COMMENT/OPER,File to copy doesn't exist.

Code Explanation

Much 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.

C1=COMMENT/INPUT
This line takes the full pathway of the file to copy and places it into the C1.INPUT
variable .

C2=COMMENT/INPUT
This line takes the full pathway of the destination file and places it into the C2.INPUT
variable

FILE/COPY,C1.INPUT,C2.INPUT,FAIL_IF_DEST_EXISTS
This line copies the original file to a destination file. This command takes three
parameters.

• Parameter 1 is C1.INPUT. This is the full path to the file to copy.


• Parameter 2 is C2.INPUT, or the full path to the destination file.
• Parameter 3, in this case, aborts the FILE/COPY procedure if it
encounters an existing file with the same destination filename. You can
set this so that it overwrites existing files of the same name.

Moving a File
The Insert | File I/O Command | File Move menu option inserts a command into the
Edit window that causes a file move operation to occur upon execution.

The syntax of this command in the Edit window is:

File/Move,<oldfilename>,<newfilename>

37
Using File Input / Output

Descriptions for some of the components of this command are:

<oldfilename>
This is the location and name of the file.

<newfilename>
This is the new location and name of the file.

To access the dialog box associated with this File I / O command:

1. Open the Edit window.


2. Place your cursor on the File Move command.
3. Press F9.

Sample Code for File Move

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

The following code asks for a file name to move and a location directory and file name
to move to. It then performs the file move operation.

C1=COMMENT/INPUT,Please type a file you want to move.


,(Include full file path)
C2=COMMENT/INPUT,Please type a destination file name.
,(Include full file path)
V1=FILE/EXISTS,C1.INPUT

38
Deleting a File

IF/V1<>0
COMMENT/OPER,File exists to move. File move commencing.
FILE/MOVE,C1.INPUT,C2.INPUT
V2=FILE/EXISTS,C2.INPUT
IF/V2==0
COMMENT/OPER,"File doesn't exist at: " + C2.INPUT
,The MOVE didn't function properly.
ROUTINE/END
END_IF/
ELSE/
COMMENT/OPER,File MOVE successful.
ROUTINE/END
END_ELSE/
END_IF/
COMMENT/OPER,Original file doesn't exist. Try again.

Code Explanation

Much of this code is similar to that explained in "Sample Code for File ".

Only explanations unique to this example are given here.

FILE/MOVE,C1.INPUT,C2.INPUT
This line copies the original file to a destination file. This command takes two
parameters.

• Parameter 1 is C1.INPUT. This is the full path to the file to move.


• Parameter 2 is C2.INPUT, or the full path to the destination file.

Deleting a File
The Insert | File I/O Command | File Delete menu option inserts a command into the
Edit window that deletes a file when the command executes.

The syntax of this command in the Edit window is:

File/Delete,<filename>

Descriptions for some of the components of this command are:

39
Using File Input / Output

<filename>
This is the name of the file to delete.

To access the dialog box associated with this File I / O command:

1. Open the Edit window.


2. Place your cursor on the File Delete command.
3. Press F9.

Sample Code for File Delete

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

The following code asks for a file name and then deletes the file.

C1=COMMENT/INPUT,Please type a file you want to delete.


,(Include full file path)
V1=FILE/EXISTS,C1.INPUT
IF/V1<>0
COMMENT/OPER,File exists. Ready to delete.
FILE/DELETE,C1.INPUT
V2=FILE/EXISTS,
IF/V2==0
COMMENT/OPER,File deleted successfully

40
Checking for a File's Existence

ROUTINE/END
END_IF/
ELSE/
COMMENT/OPER,File still exists
ROUTINE/END
END_ELSE/
END_IF/
COMMENT/OPER,File doesn't exist to delete. Choose a file
that exists.

Code Explanation

Much of this code is similar to that explained in "Sample Code for File Move ".

Only explanations unique to this example are given here.

FILE/DELETE,C1.INPUT
This line deletes the file specified. This command takes one parameter, the name of the
file to delete. In this case, C1.INPUT.

Checking for a File's Existence


The Insert | File I/O Command | File Exists menu option inserts a command into the
Edit window that checks for the existence of a file when executed and sets the supplied
variable with the result.

The syntax of this command in the Edit window is:

<varname> = File/Exists,<filename>

Descriptions for some of the components of this command are:

<filename>
This is the name of the file being checked to see if it exists on disk.

<varname>
This is the name of the variable that is set to the result of the check that is
performed. The variable is set to 1 if the file exists and 0 if the file does not exist.

To access the dialog box associated with this File I / O command:

1. Open the Edit window.

41
Using File Input / Output

2. Place your cursor on the File Exists command.


3. Press F9.

Sample Code for File Exists

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

The following code asks for a file name and then checks for the file's existence.

C1=COMMENT/INPUT,Please type a file you want to check.


V1=FILE/EXISTS,C1.INPUT
IF/V1<>0
COMMENT/OPER,File exists.
END_IF/
ELSE/
COMMENT/OPER,File doesn't exist
END_ELSE/

Code Explanation

Much 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.

42
Displaying a File Dialog Box

V1=FILE/EXISTS,C1.INPUT
This line checks to see if the specified file exists. The file must be placed in the directory
where PC-DMIS resides for this code to work; otherwise, the line containing the file
must also contain the full pathway for the file. V1 receives the result of the file check. It's
a non-zero value if it exists; 0 otherwise.

Displaying a File Dialog Box


The Insert | File I/O Command | File Dialog menu option inserts a command into the
Edit window that brings up an Open dialog box during execution. This allows the
operator to choose a file name at run time. It stores the name of the file chosen in the
specified variable.

The syntax of this command in the Edit window is:

<varname> = File/Dialog,<expr>

Descriptions for some of the components of this command are:

<varname>
This is the name of the variable that is set to the name of the file chosen by the
user in the file dialog box.

<expr>
This is the text that appears on the title bar of the file dialog box.

To access the dialog box associated with this File I / O command:

1. Open the Edit window.


2. Place your cursor on the File Dialog command.
3. Press F9.

43
Using File Input / Output

Sample Code for File Dialog

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

The following code submits a dialog box that allows you to choose a file to delete.

V1=FILE/DIALOG,Choose a file to delete.


V2=FILE/EXISTS,V1
IF/V2<>0
COMMENT/OPER,File exists. Ready to delete.
FILE/DELETE,V1
V3=FILE/EXISTS,
IF/V3==0
COMMENT/OPER,File deleted successfully
ROUTINE/END
END_IF/
ELSE/
COMMENT/OPER,File still exists
ROUTINE/END
END_ELSE/
END_IF/
COMMENT/OPER,File doesn't exist to delete. Choose a file
that exists.

Much 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.

V1=FILE/Dialog,Choose a file to delete


This line displays a dialog box with the title "Choose a file to delete". You can browse to
a file and when you click Open, PC-DMIS gives V1 the full path to the selected file. The
rest of the routine deletes the selected file.

44
Checking for the End of a File or the End of a Line

Checking for the End of a File or the End


of a Line
PC-DMIS allows you to check for the End of a File by using the functions EOF or EOL in
a conditional test.

EOF stands for END OF FILE. This function takes a file pointer of type string. When
properly placed within a conditional statement, it tests to see if the file pointer has
reached the end of the specified file. If it has, then the function returns true.

EOL stands for END OF LINE. This function takes a file pointer of type string. When
properly placed within a conditional statement, it tests to see if the file pointer has
reached the end of a line in the specified file. If it has, then the function returns true.
This works best inside of a loop.

The syntax of this command in the Edit window is:

EOF(<filepointer>) or EOL(<filepointer>)

Descriptions for some of the components of this command are:

<filepointer>
This is the name of the file pointer that you're checking.

Sample code for EOF and EOL

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

The following code opens test.txt and reads through the file. As long as the end of file
hasn't been reached (designated with the code, WHILE/!EOF), PC-DMIS reads
through the file character by character, and assigns a character to V1.

If PC-DMIS reaches the end of a line in the file, PC-DMIS shows the last character on
that line.

45
Using File Input / Output

This repeats until PC-DMIS reaches the end of the file. PC-DMIS then shows the text
"End of File Reached…".

FPTR=FILE/OPEN,D:\temp\test.txt,READ
WHILE/!EOF("FPTR")
V1=FILE/READ_CHARACTER,FPTR
IF/EOL("FPTR")
COMMENT/OPER,NO,"End of Line Reached. The last character
is:"
,V1
END_IF/
END_WHILE/
COMMENT/OPER,NO,"End of File Reached..."

46

You might also like