REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 17-Aug-2009 Edit History  

REBOL 3 Guide: Files: Reading and writing files

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.


Reading binary data

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.

Note change from R2:

Please note that files in R3 are read as binary! as the default.

This was done to prevent corruption to files for data such as images, when read accidentally as read as text. In addition, because of Unicode, even text files are now assumed to be encoded (even if they are ASCII.)

Writing a binary file

To write a binary! file, use the write function.

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.

Reading string data

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.

For example:

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

Writing string data

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.

Reading a block of lines

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.

Writing a block of lines

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 > - WIP Wiki Feedback Admin