问题
Currently I have some code in this way:
method printFuncIntoFile X =
let funHead = "head"
and funbody = "body"
and oc = open_out "test.txt" in
Printf.fprintf oc "%s%s" funHead funBody;
close_out oc;
foo X.child
And this code can only leave the last function content in the text.txt.
I search the Printf document but can only find a
val bprintf : Buffer.t -> ('a, Buffer.t, unit) format -> 'a
which requires a Buffer data structure, I think there should be some strategies more easy, even though I don't know
So my question is that:
How to print into file in OCaml in a appending way?
回答1:
You should be able to use:
open_out_gen [Open_append; Open_creat] 0o666 "test.txt"
Note that opening channels isn't part of Printf, but part of the Pervasives module.
来源:https://stackoverflow.com/questions/22993464/how-to-print-into-file-in-ocaml-in-a-appending-way