Welcome Guest (Log in)
Product Edition
read from file
Expander triangle

read from {file pathName | stdin} [at start] {until {string | end | EOF | empty} | for amount [chunkType]} [in time]

Desktop, Web and Server
Platform Support
MacOS,Mac OS X,Windows,Linux
Takes data from a file that has been opened with the open file command, and places the data in the it variable.

read from file "Test" for 8 -- reads 8 characters
read from file "COM1:" at 20 until EOF
read from file (field "Datafile") at -100 for charsToRead
read from stdin for 1 line

Additional Comments
Expander triangle

Use the read from file command to get data from a file.


The pathName specifies the name and location of the file you want to read from. It must be the same as the path you used with the open file command.

Important! The pathName is case-sensitive, even on platforms where file names are not case-sensitive. It must be exactly the same--including the case of characters--as the name you used with the open filecommand.

If you specify the name of a serial port on Mac OS or Windows systems, Revolution reads from the specified port. The names of serial ports end in a colon (:).

The start specifies the character or byte position in the file where you want to begin reading. A positive number begins start characters after the beginning of the file; a negative number begins start characters before the end of the file.

The string is any expression that evaluates to a string. When Revolution encounters the string in the file, it stops reading. If the string is not encountered, the read from file command continues reading until it reaches the end of the file.

If you specify any of EOF, end, or empty, the read continues reading until it reaches the end of the file. (If you're reading from a serial port, you must use the form read from file portname until empty.)

The amount is a positive integer and specifies how much data to read.

The chunkType is one of chars, characters, words, items, lines, int1, uInt1, int2, uint2, int4, or uint4. The read from file command reads amount of the specified chunkType. If you don't specify a chunkType, amount characters are read from the file.

The time is the time to wait for the read to be completed, in milliseconds, seconds, or ticks.


The file to read from must be opened first with the open file command, and the mode the file was opened in must be either read or update. If the file is not open or is open write-only, the result function is set to "File is not open for read.".

If you don't specify a start, Revolution begins at the position determined by the seek command, or wherever the last read from file or write to file command to that file left off, or at the first character, if the file has not been accessed since being opened, or at the last character, if the file was opened in append mode.

The until string form reads data until the specified string is encountered. The until end, until EOF, and until empty forms are synonyms, and read data up to the end of the file. You can read an entire file by opening it and reading until the end:

open file fileToRead

read from file fileToRead until EOF

close file fileToRead

The read from stdin form reads from the standard input (on Unix systems). The standard input is always open, so you can read from it without first opening it.

The data is placed in the it variable after reading. If you specified a binary data chunkType (int1, uInt1, int2, uint2, int4, or uint4), the data consists of a comma-separated list of numbers, one for the numerical value of each chunk that was read. Otherwise, the data is placed in the it variable as it appears in the file.

If the read from file command encounters the end of the file, the result function is set to "eof". If the command was successful and did not encounter the end of the file, the result function is set to empty.

Tip: As an alternative to the open file and read from filecommands, you can also use the URLkeyword with get and other commands to access the contents of a file.

Expander triangle
Lesson icon

This tutorial and example stack use the open file, read from file, close file, and write to file commands along with the specialFolderPath function to read information into a Revolution stack from an external file (in this case, a text file, but it could be a binary file if so specified), as well as write data from a Revolution stack into an external file.

Upon completion of this tutorial, you should be able to:
•Use Revolution to write a file to disk
•Use Revolution to read a file from disk
•Understand the specialFolderPath function in Transcript

Resources of Interest:

Lesson icon

This lesson will show you how to open a binary file, process binary data and write binary data to a file.

User Comments
Expander triangle