up previous next

3.7.3 File IO

To print CoCoA output to a file, one first opens the file with OpenOFile then prints to the file using Print On.

To receive verbatim input from a file, one first opens the file with OpenIFile, then gets characters from the file with Get. Actually, Get gets a list of ascii codes for the characters in the file. These can be converted to real characters using the function Ascii.

example

    
D := OpenOFile("my-file"); -- open text file with name "my-file",
                           -- creating it if necessary
Print "hello world" On D; -- append "hello world" to my-file
Close(D); -- close the file
D := OpenIFile("my-file"); -- open "my-file"
Get(D,10);  -- get the first ten characters, in ascii code
[104, 101, 108, 108, 111, 32, 119, 111, 114, 108]
-------------------------------
Ascii(It); -- convert the ascii code
hello worl
-------------------------------
Close(D);
        
    
To read and execute a sequence of CoCoA commands from a text file, one uses the Source command, or equivalently <<. For instance, if the file MyFile.coc contains a list of CoCoA commands, then
<<MyFile.coc;
reads and executes the commands.