up previous next
1.6.3 File IO
***** NOT YET UPDATED TO CoCoA-5: follow with care *****

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. For instance, if the file MyFile.coc contains a list of CoCoA commands, then
  Source "MyFile.cocoa";
reads and executes the commands.