26 - Using File Input - Output
26 - Using File Input - Output
i
Table of Contents
Copying a File............................................................................................................ 35
Deleting a File............................................................................................................ 39
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.
1
Using File Input / Output
See "Opening a File for Reading or Writing" and "Closing an Opened File after Reading
or Writing".
• 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.
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:
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.
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.
Files can be opened simply to view information or to add and save information.
3
Using File Input / Output
<filepointername> =File/Open,<filename>,<openmode>
<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.
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:
File/Close, <filepointername>,<closemode>
<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.
6
Reading a Character from a File
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
<varname> = File/ReadCharacter,<filepointername>
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.
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.
<varname> = File/ReadLine,<filepointername>,<expr>
<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.
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.
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.
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
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.
file at execution time. The amount of characters read in is indicated by the size
parameter.
<varname>=File/Read_Block,<fptrname>,<size>
<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.
16
Reading a Block of Text from a File
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:
Code Explanation
Some of this code is similar to that explained in "Sample Code for Read Character" or in
"Sample Code for Read Line".
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.
• 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).
<varname> = FILE/READ_UPTO,<fptrname>,<delimiters>
<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.
19
Using File Input / Output
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.
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
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.
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,:
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:
21
Using File Input / Output
File/Write_Character,<fptrname>,<expr>
<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.
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.
22
Writing a Character to a File
Code Explanation
Some of this code is similar to that explained in "Sample Code for Read Character" or in
"Sample Code for Read Line".
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.
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.
File/WriteLine,<fptrname>,<expr>
<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.
24
Writing a Line to a File
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.
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".
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.
File/WriteBlock,<fptrname>,<expr>
<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:
Notice that if CHR(10) is inside the quotation marks the actual text of CHR(10) gets sent
to the file.
27
Using File Input / Output
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.
Code Explanation
Some of this code is similar to that explained in "Sample Code for Read Character" or in
"Sample Code for Read Line".
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
File/Rewind,<fptrname>
<fptrname>
This is the name of the file pointer to reposition at the beginning of the file.
29
Using File Input / Output
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.
Code Explanation
Some of this code is similar to that explained in "Sample Code for Read Character" or in
"Sample Code for Read Line".
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.
File/SavePosition,<fptrname>
<fptrname>
This is the name of the file pointer whose file position is saved.
31
Using File Input / Output
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.
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".
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.
File/RecallPosition, <fptrname>
<fptrname>
This is the name of the file pointer whose position is being recalled.
33
Using File Input / Output
3. Press F9.
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.
34
Copying a File
Code Explanation
This code is similar to that explained in "Sample Code for Rewind to Start".
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.
File/Copy,<srcfilename>,<destfilename>,<replacemode>
<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.
35
Using File Input / Output
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.
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".
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.
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.
File/Move,<oldfilename>,<newfilename>
37
Using File Input / Output
<oldfilename>
This is the location and name of the file.
<newfilename>
This is the new location and name of the file.
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.
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 ".
FILE/MOVE,C1.INPUT,C2.INPUT
This line copies the original file to a destination file. This command takes two
parameters.
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.
File/Delete,<filename>
39
Using File Input / Output
<filename>
This is the name of the file to 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.
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 ".
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.
<varname> = File/Exists,<filename>
<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.
41
Using File Input / Output
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.
Code Explanation
Much of this code is similar to that explained in "Sample Code for Read Character" or in
"Sample Code for Read Line".
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.
<varname> = File/Dialog,<expr>
<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.
43
Using File Input / Output
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.
Much of this code is similar to that explained in "Sample Code for Read Character" or in
"Sample Code for Read Line".
44
Checking for the End of a File or the End of a Line
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.
EOF(<filepointer>) or EOL(<filepointer>)
<filepointer>
This is the name of the file pointer that you're checking.
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