|REBOL 3 Docs||Guide||Concepts||Functions||Datatypes||Errors|
|TOC < Back Next >||Updated: 6-Feb-2009 Edit History|
Files are read as a series of text characters or as binary bytes. The source of the file is either a local file on your system or a file from the network.
To read a local text file, use the read function:
text: read %file.txt
The read function returns a string that holds the entire text of the file. In the above example, the variable text refers to that string.
Within the string returned by read, line terminators are converted to newline characters, regardless of what style of line termination is used on your operating system. This allows you to write scripts that search for newline without concern for what particular character or characters constitute a line termination.
next-line: next find text newline
A file can also be read as separate lines that are stored in a block:
lines: read/lines %file.txt
See the [bad-link:concepts/line.txt] Conversion section for more information about newline and reading lines.
To view the contents of a text file, you can read it using read and print it using print:
print read %service.txt I wanted the gold, and I sought it,I scrabbled and mucked like a slave.
To read a binary file such as an image, a program, or a sound, use read/binary:
data: read/binary %file.bin
The read/binary function returns a binary series that holds the entire contents of the file. In the above example, the variable data refers to the binary series. No conversion of any type is done to the file.
Files can be read from a network. For example, to view a text file from a network using the HTTP protocol:
print read http://www.rebol.com/test.txt Hellotherenewuser!
The file could be written locally with the line:
write %test.txt read http:/www.rebol.com/test.txt
In the write process the file will have its line termination converted to that which is used by your operating system.
To read and save a binary file, such as an image, use the following line:
write %image.jpg read/binary http:/www.rebol.com/image.jpg
Refer to the chapter on [bad-link:concepts/network.txt] Protocols for more information and examples of accessing files across networks.
|TOC < Back Next >||REBOL.com - WIP Wiki||Feedback Admin|