Writing to files
Writing to a text file:
action! write Red-by-example MyCode4fun
Writes to a file, creating it if it doesn't exist.
>> write %myFirstFile.txt "Once upon a time..."
Appending a text file:
/append
If you just write again to the file created above, 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:
/lines
>> 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, use write/seek %<file> <n> :
>> write/seek %myFirstFile.txt "NEW TEXT" 5
Now the first file has: "Once NEW TEXTime...there was a house."
Write refinements:
/binary => Preserves contents exactly.
/lines => Write each value in a block as a separate line.
/info =>
/append => Write data at end of file.
/part => Partial write a given number of units.
/seek => Write at a specific position.
/allow => Specifies protection attributes.
/as => Write with the specified encoding, default is 'UTF-8.
function! save Red-by-example MyCode4fun
Saves a value, block, or other data to a file, URL, binary, or string.
Difference between write and 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
do, load and save are better understood if you think of Red's console as the screen of some old computer from the 80's running some variation of basic language. You can load your program, save it, or do (execute) it.