Here are some more useful examples of reading/writing to files.
/* Print all words in a text file named "mytext.txt" */
filename = 'C:\mytext.txt'
DO
/* Open the file for reading, but only if it exists */
STREAM(filename, 'C', 'OPEN READ')
/* Any more lines? */
DO WHILE LINES(filename, 'N') > 0
/* Read the next line */
line = LINEIN(filename, , 1)
/* Count how many words in this line */
words = WORDS(line)
/* SAY each word */
DO i = 1 TO words
SAY WORD(line, i)
END
END /* WHILE LINES() */
CATCH NOTREADY
CONDITION('M')
/* Close the file when the DO/END is done */
FINALLY
STREAM(filename, 'C', 'CLOSE')
END
/* Read all the lines of a text file into a
* stem variable named MyVar, where MyVar.0 tells how
* many lines there are, and MyVar.1 to MyVar.xxx are
* those lines.
*/
filename = 'C:\mytext.txt'
/* Initially no lines read */
lineno = 0
/* Open the file for reading, but only if it exists */
IF STREAM(filename, 'C', 'OPEN READ') == 'READY:' THEN DO
/* Set up for any errors */
SIGNAL ON NOTREADY NAME FileError
/* Any more lines? */
DO WHILE LINES(filename, 'N') > 0
i = lineno + 1
/* Read the next line */
MyVar.i = LINEIN(filename, , 1)
/* Increment line count now that it is successfully read */
lineno = i
END /* WHILE LINES() */
FileError:
/* Close the file */
STREAM(filename, 'C', 'CLOSE')
END /* STREAM() */
/* File can't be opened. SAY why */
ELSE SAY STREAM(filename, 'D')
/* Store line count */
MyVar.0 = lineno
Note that Reginald offers a LOADTEXT() function that does all of the above with a single call as so:
/* Read all the lines of a text file into a
* stem variable named MyVar, where MyVar.0 tells how
* many lines there are, and MyVar.1 to MyVar.xxx are
* those lines.
*/
filename = 'C:\mytext.txt'
DO
LOADTEXT('MyVar.', filename)
CATCH NOTREADY
CONDITION('M')
END
/* Write out all the lines to a text file from
* a stem variable named MyVar, where MyVar.0 tells how
* many lines there are, and MyVar.1 to MyVar.xxx are
* those lines.
*/
filename = 'C:\mytext.txt'
/* Initially no lines written */
lineno = 0
/* Open/create the file, and overwrite any old contents */
IF STREAM(filename, 'C', 'OPEN WRITE REPLACE') == 'READY:' THEN DO
/* Any more lines? */
DO WHILE lineno < MyVar.0
/* Increment line count */
lineno = lineno + 1
/* Write the next line */
IF LINEOUT(filename, MyVar.lineno) \= 0 THEN DO
/* Display error message */
SAY 'Error writing to "'||filename||'"'
SAY STREAM(filename, 'D')
/* Cause the DO WHILE to END */
lineno = MyVar.0
END
END /* lineno < MyVar.0 */
/* Close the file */
STREAM(filename, 'C', 'CLOSE')
END /* STREAM() */
/* File can't be opened. SAY why */
ELSE SAY STREAM(filename, 'D')
/* Read the entire contents of a file into a
* variable named MyVar.
*/
filename = 'C:\mytext.txt'
/* Initially no bytes read */
size = 0
/* Open the file for reading, but only if it exists */
IF STREAM(filename, 'C', 'OPEN READ') == 'READY:' THEN DO
/* Get the size in bytes */
MyVar = STREAM(filename, 'C', 'QUERY SIZE')
/* Anything to read in? */
IF MyVar \= "" THEN DO
size = MyVar
/* Read in the bytes */
MyVar = CHARIN(filename, 1, size)
IF LENGTH(MyVar) \= size THEN DO
/* Display error message */
SAY 'Error reading from "'||filename||'"'
SAY STREAM(filename, 'D')
END
END /* MyVar \= "" */
/* Close the file */
STREAM(filename, 'C', 'CLOSE')
END /* STREAM() */
/* File can't be opened. SAY why */
ELSE SAY STREAM(filename, 'D')
/* size = how many bytes, MyVar = contents of file */