### 91.2 String Input and Output

Example: Formatted printing to a file.

    (%i1) s: openw("file.txt");
    (%o1)                      #<output stream file.txt>
    (%i2) control:
    "~2tAn atom: ~20t~a~%~2tand a list: ~20t~{~r ~}~%~2t\
    and an integer: ~20t~d~%"$
    (%i3) printf( s,control, 'true,[1,2,3],42 )$
    (%o3)                                false
    (%i4) close(s);
    (%o4)                                true
    (%i5) s: openr("file.txt");
    (%o5)                      #<input stream file.txt>
    (%i6) while stringp( tmp:readline(s) ) do print(tmp)$
      An atom:          true
      and a list:       one two three
      and an integer:   42
    (%i7) close(s)$

Function: close (stream)

  Closes stream and returns `true` if stream had been open.

  Categories:File input ·File output ·Package stringproc ·

Function: flength (stream)

  stream has to be an open stream from or to a file. `flength` then returns the number of bytes which are currently present in this file.

  Example: See writebyte .

  Categories:File input ·File output ·Package stringproc ·

Function: flush_output (stream)

  Flushes stream where stream has to be an output stream to a file.

  Example: See writebyte .

  Categories:File output ·Package stringproc ·

Function: fposition
`fposition` (stream)
`fposition` (stream, pos)

  Returns the current position in stream, if pos is not used. If pos is used, `fposition` sets the position in stream. stream has to be a stream from or to a file and pos has to be a positive number.

  Positions in data streams are like in strings or lists 1-indexed, i.e. the first element in stream is in position 1.

  Categories:File input ·File output ·Package stringproc ·

Function: freshline
`freshline` ()
`freshline` (stream)

  Writes a new line to the standard output stream if the position is not at the beginning of a line and returns `true`. Using the optional argument stream the new line is written to that stream. There are some cases, where `freshline()` does not work as expected.

  See also newline.

  Categories:File output ·Package stringproc ·

Function: get_output_stream_string (stream)

  Returns a string containing all the characters currently present in stream which must be an open string-output stream. The returned characters are removed from stream.

  Example: See make_string_output_stream .

  Categories:Package stringproc ·

Function: make_string_input_stream
`make_string_input_stream` (string)
`make_string_input_stream` (string, start)
`make_string_input_stream` (string, start, end)

  Returns an input stream which contains parts of string and an end of file. Without optional arguments the stream contains the entire string and is positioned in front of the first character. start and end define the substring contained in the stream. The first character is available at position 1.

      (%i1) istream : make_string_input_stream("text", 1, 4);
      (%o1)              #<string-input stream from "text">
      (%i2) (while (c : readchar(istream)) # false do sprint(c), newline())$
      t e x
      (%i3) close(istream)$

  Categories:Package stringproc ·

Function: make_string_output_stream ()

  Returns an output stream that accepts characters. Characters currently present in this stream can be retrieved by get_output_stream_string.

      (%i1) ostream : make_string_output_stream();
      (%o1)               #<string-output stream 09622ea0>
      (%i2) printf(ostream, "foo")$

      (%i3) printf(ostream, "bar")$

      (%i4) string : get_output_stream_string(ostream);
      (%o4)                            foobar
      (%i5) printf(ostream, "baz")$

      (%i6) string : get_output_stream_string(ostream);
      (%o6)                              baz
      (%i7) close(ostream)$

  Categories:Package stringproc ·

Function: newline
`newline` ()
`newline` (stream)

  Writes a new line to the standard output stream. Using the optional argument stream the new line is written to that stream. There are some cases, where `newline()` does not work as expected.

  See sprint for an example of using `newline()`.

  Categories:File output ·Package stringproc ·

Function: opena (file)

  Returns a character output stream to file. If an existing file is opened, `opena` appends elements at the end of file.

  For binary output see opena_binary .

  Categories:File output ·Package stringproc ·

Function: openr
`openr` (file)
`openr` (file, encoding)

  Returns a character input stream to file. `openr` assumes that file already exists. If reading the file results in a lisp error about its encoding passing the correct string as the argument encoding might help. The available encodings and their names depend on the lisp being used. For sbcl a list of suitable strings can be found at http://www.sbcl.org/manual/#External-Formats.

  For binary input see openr_binary . See also `close` and `openw`.

      (%i1) istream : openr("data.txt","EUC-JP");
      (%o1)     #<FD-STREAM for "file /home/gunter/data.txt" {10099A3AE3}>
      (%i2) close(istream);
      (%o2)                                true

  Categories:File input ·Package stringproc ·

Function: openw (file)

  Returns a character output stream to file. If file does not exist, it will be created. If an existing file is opened, `openw` destructively modifies file.

  For binary output see openw_binary .

  See also `close` and `openr`.

  Categories:File output ·Package stringproc ·

Function: printf
`printf` (dest, string)
`printf` (dest, string, expr_1, ..., expr_n)

  Produces formatted output by outputting the characters of control-string string and observing that a tilde introduces a directive. The character after the tilde, possibly preceded by prefix parameters and modifiers, specifies what kind of formatting is desired. Most directives use one or more elements of the arguments expr_1, ..., expr_n to create their output.

  If dest is a stream or `true`, then `printf` returns `false`. Otherwise, `printf` returns a string containing the output. By default the streams stdin, stdout and stderr are defined. If Maxima is running as a network client (which is the normal case if Maxima is communicating with a graphical user interface, which must be the server) `setup-client` will define old_stdout and old_stderr, too.

  `printf` provides the Common Lisp function `format` in Maxima. The following example illustrates the general relation between these two functions.

      (%i1) printf(true, "R~dD~d~%", 2, 2);
      R2D2
      (%o1)                                false
      (%i2) :lisp (format t "R~dD~d~%" 2 2)
      R2D2
      NIL

  The following description is limited to a rough sketch of the possibilities of `printf`. The Lisp function `format` is described in detail in many reference books. Of good help is e.g. the free available online-manual "Common Lisp the Language" by Guy L. Steele. See chapter 22.3.3 there.

  In addition, `printf` recognizes two format directives which are not known to Lisp `format`. The format directive `~m` indicates Maxima pretty printer output. The format directive `~h` indicates a bigfloat number.

         ~%       new line
         ~&       fresh line
         ~t       tab
         ~$       monetary
         ~d       decimal integer
         ~b       binary integer
         ~o       octal integer
         ~x       hexadecimal integer
         ~br      base-b integer
         ~r       spell an integer
         ~p       plural
         ~f       floating point
         ~e       scientific notation
         ~g       ~f or ~e, depending upon magnitude
         ~h       bigfloat
         ~a       uses Maxima function string
         ~m       Maxima pretty printer output
         ~s       like ~a, but output enclosed in "double quotes"
         ~~       ~
         ~<       justification, ~> terminates
         ~(       case conversion, ~) terminates
         ~[       selection, ~] terminates
         ~{       iteration, ~} terminates

  Note that the directive ~* is not supported.

  If dest is a stream or `true`, then `printf` returns `false`. Otherwise, `printf` returns a string containing the output.

      (%i1) printf( false, "~a ~a ~4f ~a ~@r",
                    "String",sym,bound,sqrt(12),144), bound = 1.234;
      (%o1)                 String sym 1.23 2*sqrt(3) CXLIV
      (%i2) printf( false,"~{~a ~}",["one",2,"THREE"] );
      (%o2)                          one 2 THREE
      (%i3) printf(true,"~{~{~9,1f ~}~%~}",mat ),
                mat = args(matrix([1.1,2,3.33],[4,5,6],[7,8.88,9]))$
            1.1       2.0       3.3
            4.0       5.0       6.0
            7.0       8.9       9.0
      (%i4) control: "~:(~r~) bird~p ~[is~;are~] singing."$
      (%i5) printf( false,control, n,n,if n=1 then 1 else 2 ), n=2;
      (%o5)                    Two birds are singing.

  The directive ~h has been introduced to handle bigfloats.

      ~w,d,e,x,o,p@H
       w : width
       d : decimal digits behind floating point
       e : minimal exponent digits
       x : preferred exponent
       o : overflow character
       p : padding character
       @ : display sign for positive numbers

      (%i1) fpprec : 1000$
      (%i2) printf(true, "|~h|~%", 2.b0^-64)$
      |0.0000000000000000000542101086242752217003726400434970855712890625|
      (%i3) fpprec : 26$
      (%i4) printf(true, "|~h|~%", sqrt(2))$
      |1.4142135623730950488016887|
      (%i5) fpprec : 24$
      (%i6) printf(true, "|~h|~%", sqrt(2))$
      |1.41421356237309504880169|
      (%i7) printf(true, "|~28h|~%", sqrt(2))$
      |   1.41421356237309504880169|
      (%i8) printf(true, "|~28,,,,,'*h|~%", sqrt(2))$
      |***1.41421356237309504880169|
      (%i9) printf(true, "|~,18h|~%", sqrt(2))$
      |1.414213562373095049|
      (%i10) printf(true, "|~,,,-3h|~%", sqrt(2))$
      |1414.21356237309504880169b-3|
      (%i11) printf(true, "|~,,2,-3h|~%", sqrt(2))$
      |1414.21356237309504880169b-03|
      (%i12) printf(true, "|~20h|~%", sqrt(2))$
      |1.41421356237309504880169|
      (%i13) printf(true, "|~20,,,,'+h|~%", sqrt(2))$
      |++++++++++++++++++++|

  For conversion of objects to strings also see `concat`, `sconcat`, `string` and `simplode`.

  Categories:File output ·Package stringproc ·

Function: readbyte (stream)

  Removes and returns the first byte in stream which must be a binary input stream. If the end of file is encountered `readbyte` returns `false`.

  Example: Read the first 16 bytes from a file encrypted with AES in OpenSSL.

      (%i1) ibase: obase: 16.$

      (%i2) in: openr_binary("msg.bin");
      (%o2)                       #<input stream msg.bin>
      (%i3) (L:[],  thru 16. do push(readbyte(in), L),  L:reverse(L));
      (%o3) [53, 61, 6C, 74, 65, 64, 5F, 5F, 88, 56, 0DE, 8A, 74, 0FD,
             0AD, 0F0]
      (%i4) close(in);
      (%o4)                                true
      (%i5) map(ascii, rest(L,-8));
      (%o5)                      [S, a, l, t, e, d, _, _]
      (%i6) salt: octets_to_number(rest(L,8));
      (%o6)                          8856de8a74fdadf0

  Categories:File input ·Package stringproc ·

Function: readchar (stream)

  Removes and returns the first character in stream. If the end of file is encountered `readchar` returns `false`.

  Example: See make_string_input_stream.

  Categories:File input ·Package stringproc ·

Function: readline (stream)

  Returns a string containing all characters starting at the current position in stream up to the end of the line or `false` if the end of the file is encountered.

  Categories:File input ·Package stringproc ·

Function: sprint (expr_1, …, expr_n)

  Evaluates and displays its arguments one after the other ‘on a line’ starting at the leftmost position. The expressions are printed with a space character right next to the number, and it disregards line length. `newline()` might be used for line breaking.

  Example: Sequential printing with `sprint`. Creating a new line with `newline()`.

      (%i1) for n:0 thru 19 do sprint(fib(n))$
      0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
      (%i2) for n:0 thru 22 do (
               sprint(fib(n)),
               if mod(n,10) = 9 then newline() )$
      0 1 1 2 3 5 8 13 21 34
      55 89 144 233 377 610 987 1597 2584 4181
      6765 10946 17711

  Categories:Package stringproc ·

Function: writebyte (byte, stream)

  Writes byte to stream which must be a binary output stream. `writebyte` returns `byte`.

  Example: Write some bytes to a binary file output stream. In this example all bytes correspond to printable characters and are printed by `printfile`. The bytes remain in the stream until `flush_output` or `close` have been called.

      (%i1) ibase: obase: 16.$

      (%i2) bytes: map(cint, charlist("GNU/Linux"));
      (%o2)                [47, 4E, 55, 2F, 4C, 69, 6E, 75, 78]
      (%i3) out: openw_binary("test.bin");
      (%o3)                      #<output stream test.bin>
      (%i4) for i thru 3 do writebyte(bytes[i], out);
      (%o4)                                done
      (%i5) printfile("test.bin")$

      (%i6) flength(out);
      (%o6)                                  0
      (%i7) flush_output(out);
      (%o7)                                true
      (%i8) flength(out);
      (%o8)                                  3
      (%i9) printfile("test.bin")$
      GNU
      (%i0A) for b in rest(bytes,3) do writebyte(b, out);
      (%o0A)                               done
      (%i0B) close(out);
      (%o0B)                               true
      (%i0C) printfile("test.bin")$
      GNU/Linux

  Categories:File output ·Package stringproc ·