Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml How to write into file?

I'm looking for a way to write two ints to a file. There will be many pairs of two ints. Between the two numbers there should be a space (I mean ''). For example, something like this:

1 2
6 896
243 865
....
like image 796
user2900510 Avatar asked Oct 20 '13 16:10

user2900510


2 Answers

You can use something like this:

let rec print_numbers oc = function 
  | [] -> ()
  | e::tl -> Printf.fprintf oc "%d %d\n" (fst e) (snd e); print_numbers oc tl

let () =
  let nums = [(1, 2); (6, 896); (243, 865)] in
  let oc = open_out "filename.txt" in
  print_numbers oc nums;
  close_out oc;

This assumes your data is a list of pairs.

like image 149
Dave L. Avatar answered Sep 24 '22 07:09

Dave L.


If you use Core, you can do this:

open Core.Std
let () = Out_channel.write_all "your_file.txt" ~data:"Your text"
like image 25
Matthias Braun Avatar answered Sep 21 '22 07:09

Matthias Braun