|REBOL 3 Docs||Guide||Concepts||Functions||Datatypes||Errors|
|TOC < Back Next >||Updated: 17-Aug-2009 Edit History|
For binary data and strings, you can use read and write to transfer data to and from files. For other types of data, it's easier to use load and save, as described in the files: loading and saving files section.
There are several ways to read a file. For small files, you can simply read them directly with a line like:
data: read %image.jpg
This works for files up to a few megabytes. Beyond that, it's better to break the read up into sections (using open). More on that later.
The data above is the binary! contents of the file. No conversions are made to the data. Thus, any kind of file can be read this way, including images, programs, sounds, encoded text, etc.
This line will write the data read in the above section to a new file, or it will overwrite the file if it already exists:
write %new-image.jpg data
The data must be binary! to be written as binary.
So, to copy an image file, you can simply write:
write %new-image.jpg read %image.jpg
Of course, keep in mind that for large files, a different technique should be used to reduce memory requirements.
If you want to read a file as string, you can use the /string refinement to indicate that you want the necessary Unicode (UTF-8) and line conversions.
notes: read/string %notes.txt
Line terminators will be converted to the standard format used within REBOL strings (a single LF character at the end of each line.)
When writing strings, the write function automatically detects that they are strings, and preforms the necessary conversion to UTF-8 format. No /string refinement is needed.
So, to write the above notes string:
write %notes2.txt notes
The line terminators will also be converted to the local format. On PCs, it will be CRLF, but on Linux and other systems it will be LF.
So, you can easily use REBOL to convert a text file from one line termination format to the format used on your local system:
write %notes-local.txt read/string %notes.txt
Note that this is slightly different from REBOL 2, because of the change that makes file reads binary by default. The /string refinement must be used here.
Another option is to read a file as a block of strings. Each line is a separate string within the block. This format is handy at times for processing files on a line-by-line basis.
doc: read/lines %data.txt foreach line doc [print line]
Note that each line is a string and does not contain the line terminator.
To write a block where each value of the block is placed on a separate line:
write/lines %data.txt doc
If you use the read function shown above with the /lines refinement, then each string is written as a line. However, if the block contains other values, the form function will be used to convert them to a string first.
Here's a simple example that removes all blank lines from a file:
data: read/lines %data.txt remove-each line data [empty? line] write/lines %data-small.txt data
Note that if a line contains only spaces (or tabs) it is not empty, and will not be removed.
|TOC < Back Next >||REBOL.com - WIP Wiki||Feedback Admin|