core

#### 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

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.

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