Files and I/O


Reading and writing files Paths, directories and files
Writing a text file Path names
Appending a text file A graphic file selector
Writing a series to a file making each element a line A graphic directory selector
Appending full lines Deleting a file
Replacing characters in a file Getting the size of a file
Difference between save and write Other directory and path functions
Reading files as text
Reading files as series where every line is an element
Reading files as a series where every word (separated by space) is an element
Reading and writing binary files

Path names

File paths are written with a percent sign (%) followed by a sequence of directory names that are each separated by a forward slash (/). In Windows, Red makes all the conversions from backslashes to forward slashes, you don't have to worry.

Just remembering:

  • / is the root of the current drive;
  • ./ is the current directory;
  • ../ is the parent of the current directory;
  • file paths that do not begin with a forward slash (/) are relative paths;
  • to refer to Window's often used "C" drive you should use: %/C/docs/file.txt
  • absolute paths should be avoided to ensure machine-independent scripts;

Writing to files

Lets create a text-file named "myFirstFile.txt" with the text "Once upon a time...". We will create it in the directory where the Red interpreter (Red .exe) is:

Writing to a text file:
write
>> write %myFirstFile.txt "Once upon a time..."
>>
Appending a text file:
append

If you write again to this file it will be overwritten. If you want to add more text to it (append it):

>> write/append %myFirstFile.txt "there was a house."
>>

Your file now has "Once upon a time...there was a house" in it.

Writing a series to a file making each element a line:

Now lets add another file with 3 lines of text:

>> write/lines %mySecondFile.txt ["First line;" "Second line;" "Third line."]
>>
Appending full lines:
>>  write/append/lines %mySecondFile.txt ["Fourth line;" "Fifth line;" "Sixth line."]
>>

Your file now looks like this:

First line;
Second line;
Third line.
Fourth line;
Fifth line;
Sixth line.

Notice that you could have written write/lines/append. The order of the refinements makes no difference.

Replacing characters in a file:

To replace characters in a text file, starting at n+1 position, usewrite/seek %<file> <n> :

>> write/seek %myFirstFile.txt "NEW TEXT" 5
>>

Now the first file has: "Once NEW TEXTime...there was a house."

Difference between save and write:
save
>> write %myFourthFile.txt [11 22 "three" "four" "five"]

Your file now has: [11 22 "three" "four" "five"]

>> save %myFourthFile.txt [11 22 "three" "four" "five"]

Your file now has 11 22 "three" "four" "five"

An important use of save is to simplify the saving of Red scripts that can be interpreted using the action do :

>> save %myProgram.r [Red[] print "hello"]
>> do %myProgram.r
hello

Reading files

Reading files as text:
read
>> a: read %mySecondFile.txt
== {First line;^/Second line;^/Third line.^/Fourth line;^/Fifth li

Now the variable "a" has the entire content of the file:

>> print a
First line;
Second line;
Third line.
Fourth line;
Fifth line;
Sixth line.

>>
Reading files as series where every line is an element:

Notice that the variable "a" so far is just text with newlines. If you want to read the file as a series! having each line as an element, you should use read/lines:

>> a: read/lines %mySecondFile.txt
== ["First line;" "Second line;" "Third line." "Fourth line;"...
>> print pick a 2
Second line;
Reading files as a series where every word (separated by space) is an element:
load

In this case, you should use load instead of read:

>> a: load  %mySecondFile.txt
== [First line Second line Third line. 
    Fourth line Fifth...
>> print pick a 2
line
Reading and writing binary files:

To read or write a binary file such as an image or a sound, you should use the /binary refinement. The following code loads a bitmap image to variable a and saves that image with another name:

>> a: read/binary %heart.bmp
== #{
424D660700000000000036000000280000001E00000014000000010...
>> write/binary %newheart.bmp a

Path, directories and files

A graphic file selector:
request-file

request-file opens a graphic file selector and returns the full file path as a file!

>> request-file

== %/C/Users/André/Documents/RED/myFirstFile.txt

Refinements

/title - window title. Example: request-file/title "My file is:"
/file - Default file name or directory. Example: request-file/file %"MyFile.txt"
/filter -Supply a block of filters consisting of pairs of filter names, and the actual filters. Example: request-file/filter ["executables" "*.exe" "text files" "*.txt"]
/save - File save mode. Example with filters: request-file/save/filter ["executables" "*.exe" "text files" "*.txt"]
/multi - Allows multiple file selection, returned as a block.

A graphic directory selector:
request-dir

request-dir opens a graphic directory selector and returns the full file path as a file!

== %/C/Users/André/Documents/RED/

Refinements

/title - To be displayed under the title bar.
/dir - Set starting directory.
/keep - Keep previous directory path

Deleting a file:
delete

Deletes a file and returns true if successful, false otherwise.

>> delete %file.txt
== true
Getting the size of a file:
size?

Returns the number of bytes a file has, or none if file does not exist.

>> size? %myFirstFile.txt
== 37

Other directory and path functions:

cd or change-dir - Changes the current directory.

dir, ls or list-dir - Lists the contents of a given directory. If no argument is given, lists the current directory.

dir? - Returns true if the supplied name is a valid file path!,
otherwise returns false.

dirize - Turns its argument into a valid directory.
The argument can be of file! string! url!.
Effectively dirize only appends a trailing / if needed.

exists? - Returns true if its argument is an existing path!
or false otherwise.

file? - Returns true if its argument is a file!.

get-current-dir - Returns the current directory the program is using.

get-path? - Returns true if its argument is a get-path!

path? - Returns true if its argument is a path!

split-path - Splits a file! or url! path. Returns a block containing path and
target.

suffix? - Returns the sufix of a fila. e.g: exe, txt

what-dir - Returns the current directory path as a file! value.

to-red-file - Converts a local file system path to Red's
standard machine independent path format.

to-local-file - Converts standard, system independent Red
file paths to the file format used by the local operating system.

clean-path - Cleans-up '.' and '..' in a path
and returns the cleaned path.

red-complete-file

red-complete-path

set-current-dir

results matching ""

    No results matching ""