up previous next
1.8.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 reads lines from the file with GetLine .

/**/  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"
/**/  GetLine(D);
hello world
/**/  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.