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

REBOL 3 Guide: Files: Loading and saving files


Loading data from files

The load function reads raw binary data and converts it to useful datatypes.

For example, you can load REBOL text files, both code and data, with a line like:

data: load %data.r

If data.r contains multiple values, the result of load will be a block.

For example, take a file that contains the text:

test 1234 4:25 <test> "test"

If you load it and probe the results:

probe load %example.r
[test 1234 4:25 <test> "test"]

The data is not evaluated, it's just data that you can now use in your code:

data: load %example.r
foreach item data [
    probe item

Note that if the file only contains a single value like an integer or date, the result will be that value.

You also use load for other datatypes, such as images:

image: load %photo.jpg

Here the photo.jpg file is read and converted from an encoded JPEG image (a binary! format) into the standard RGBA image! format used by REBOL.

The load function contains many other options. (Click on its name to see the details.)

Saving data to files

Quite often you will use the save function to save values to a file that you will load and use later.

To save some data:

data: [test 1234 4:25 <test> "test"]
save %data.r data

The data.r file will be created, or if it exists, it will be overwritten.

If you examine the file, you will find:

test 1234 4:25 <test> "test"

Which of course can be reloaded with load (as shown above):

data2: load %data.r
probe data2
[test 1234 4:25 <test> "test"]

The save function contains many other options. (Click on its name to see the details.)

Loading from the Internet

In addition to loading local files, you can also load files from a web server (or from any other network protocol that allows it.)

This makes it easy to obtain shared data that may be posted to various web sites.

Here's an example:

data: load
probe data
    title "REBOL Central Folder"
    summary "The portal to REBOL sites, demos, tests, and more."
    folder "Demos" %view/demos/index.r
    info "Demos of REBOL/View"

Saving to the Internet

You can also save data to a web server, if it provides a method for accepting the data (such as a web CGI script).

Here's an example:

data: [test 1234 4:25 <test> "test"]
save data

The data is sent to the server.

If you try this, you will notice that save returned a result. It is the response from the server. Because it's also in REBOL format, you can view it easily with the load function:

load save data
[ok 28 bytes]

The save-data.r script on the server is confirming that it received 28 bytes of data. (The server does nothing else with the data.)

Loading headers also

All REBOL programs require a header block. The header provides the title, version, author, and other useful meta-data about the program.

To load the header, use the /header refinement.

An example for a local file is:

prog: load/header %script.r

or, for a remote file on a web server:

prog: load/header

The first value in prog is the header object!.

For example, let's look at the header of a program:

prog: load/header
print first prog
Title: "REBOL Quick and Dirty Speed Test"
Name: 'speed-test
Type: none
Version: 1.2.0

For more information about headers, see the scripts: headers section.

  TOC < Back Next > - WIP Wiki Feedback Admin