ファイルに追加出力する

open_out を使ってファイルを開くと,そのファイルがすでに存在した場合,中身を消去してしまう。既存のファイルに追加するには,open_out_gen を使ってチャネルを作る*1

 # let outfile = open_out_gen [Open_wronly; Open_append; Open_text] 0o666 "C:/home/takatoh/foo.txt";;
 val outfile : out_channel = <abstr>

第1引数は open_flag といって,ファイルを開くときのオプション。上で設定してるのはライトオンリー,追加,テキストファイル,というところだろう。第2引数はファイルのパーミッション,第3引数はファイル名。で,かえってくるのは open_out と同じ出力用のチャネル(out_channel)。

 # output_string outfile "foo\n";;
 - : unit = ()
 # output_string outfile "bar\n";;
 - : unit = ()
 # close_out outfile;;
 - : unit = ()
 # open_out_gen;;
 - : open_flag list -> int -> string -> out_channel = <fun>

foo.txt はこうなる。

 ^o^ >type foo.txt
 Hello, world.
 foo
 bar

ちゃんと追加になっている。


open_flag はヴァリアントで,The Objective Caml system release 3.1019.2 Module Pervasives: the initially opened module の General output functionsの項によると:

 type open_flag =
 | 	Open_rdonly 	(*	open for reading.	*)
 | 	Open_wronly 	(*	open for writing.	*)
 | 	Open_append 	(*	open for appending: always write at end of file.	 *)
 | 	Open_creat 	(*	create the file if it does not exist.	*)
 | 	Open_trunc 	(*	empty the file if it already exists.	*)
 | 	Open_excl 	(*	fail if Open_creat and the file already exists.	*)
 | 	Open_binary 	(*	open in binary mode (no conversion).	*)
 | 	Open_text 	(*	open in text mode (may perform conversions).	*)
 | 	Open_nonblock 	(*	open in non-blocking mode.	*)

*1:ここ,「プログラム in OCaml」に誤植あり。○ Open_wronly,× Open_wonly