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

To print CoCoA output to a string, on may use OpenOString to open the string, then print on to write to it. To read from a string, one may open the string for input with OpenIString then get characters from it with Get .

Example
  S := "hello world";
  D := OpenIString("", S);  -- open the string S for input to CoCoA
                -- the first argument is just a name for the device
  L := Get(D,7);  -- read 7 characters from the string
  L;  -- ASCII code
[104, 101, 108, 108, 111, 32, 119]
-------------------------------
  ascii(L); -- convert ASCII code to characters
hello w
-------------------------------
  Close(D);  -- close device D
  D := OpenOString("");  -- open a string for output from CoCoA
  L := [1,2,3]; -- a list
  Print L On D;  -- print to D
  D;
record[Name := "", Type := "OString", Protocol := "CoCoALanguage"]
-------------------------------
  S := Cast(D, STRING);  -- S is the string output printed on D
  S; -- a string
  [1, 2, 3]
  Print " more characters" On D;  -- append to the existing output string
  Cast(D, STRING);
[1, 2, 3] more characters
-------------------------------
There are usually more direct ways to collect results in strings. For instance, if the output of a CoCoA command is not already of type STRING, one may convert it to a string using sprint .