How to print into file in OCaml in a appending way?

一笑奈何 提交于 2020-07-23 10:22:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!