Next: , Previous: Introduction to string processing, Up: stringproc   [Contents][Index]

79.2 Functions and Variables for input and output

例:

(%i1) s: openw("E:/file.txt");
(%o1)                     #<output stream E:/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("E:/file.txt");
(%o5)                     #<input stream E:/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)$
関数: close (stream)

streamを閉じて、 もしstreamが開いていたら trueを返します。

関数: flength (stream)

streamの中の要素の数を返します。

関数: fposition (stream)
関数: fposition (stream, pos)

もしposが使われてないなら、 streamの中の現在位置を返します。 もしposが使われているなら、 fpositionstreamの中の位置を設定します。 posは正の数でなければいけません。 streamの最初の要素は位置1にあります。

関数: freshline ()
関数: freshline (stream)

もし位置が行の最初でないなら (streamに)改行を書きます。 newlineも参照してください。

関数: newline ()
関数: newline (stream)

(streamに)改行を書きます。 newline()の使用の例に関しては、 sprintを参照してください。 newline()が期待通りに動かない いくつかの場合があることに注意してください。

関数: opena (file)

fileへの出力ストリームを返します。 もし存在しているファイルを開いたら、 openaはファイルの終わりに要素を追加します。

関数: openr (file)

fileへの入力ストリームを返します。 もしfileが存在しないなら、生成されます。

関数: openw (file)

fileへの出力ストリームを返します。 もしfileが存在しないなら、生成されます。 もし存在しているファイルを開いたら、openwfileを破壊的に変更します。

関数: printf (dest, string)
関数: printf (dest, string, expr_1, ..., expr_n)

Common Lisp関数FORMATをMaximaで利用可能にします。 (gcl.infoから: 「フォーマットは 制御文字列の文字を出力し、波形記号がディレクティブを挿入することを守ることで、 フォーマットされた出力を生成します。 波形記号の後の文字は多分、前置パラメータと修飾語が先行し、 望まれるフォーマットの種類を指定します。 ほとんどのディレクティブは、出力を生成するために1つまたは複数の引数を使います。」)

以下の記述と例はprintfを使うアイデアを与えるかもしれません。 さらなる情報に関して、Lisp参考文献を参照してください。

   ~%       改行
   ~&       行のフレッシュ
   ~t       タブ
   ~$       通貨記号
   ~d       10進整数
   ~b       2進整数
   ~o       8進整数
   ~x       16進整数
   ~br      b進整数
   ~r       整数を一字一字
   ~p       複数形
   ~f       浮動小数点
   ~e       科学的記数法
   ~g       大きさに応じて~fまたは~e
   ~h       多倍長浮動小数点
   ~a       Maxima関数文字列を使う
   ~s       ~aと同様, しかし"ダブルコーテーション"で囲まれた出力
   ~~       ~
   ~<       行揃え, ~> 終端
   ~(       大文字小文字変換, ~) 終端
   ~[       選択, ~] 終端
   ~{       繰り返し, ~} 終端

選択ディレクティブ~[は添字がゼロから始まることに注意してください。 ディレクティブ~*がサポートされていないことにも注意してください。

(%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 0 else 1 ), n=2;
(%o5)                    Two birds are singing.

もしdestがストリームかtrueなら、 printffalseを返します。 そうでないなら、printfは出力を含む文字列を返します。

関数: readline (stream)

streamの現在位置から行の終わりまでの文字を含む文字列か、 もしファイルの終わりが来たらfalseを返します。

関数: sprint (expr_1, …, expr_n)

引数を順に評価し、一番左から始まる「一行」に表示します。 数は数の右隣に’-’と共に印字され、行の長さを無視します。 もし中間行ブレークを置きたいなら、 newline()stringproc.lisp から自動ロードされます― が役に立つかもしれません。

例:

(%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 

Next: , Previous: Introduction to string processing, Up: stringproc   [Contents][Index]