Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 527

Reading integers from file OCaml

$
0
0

I have to read integers from a "file.txt" in OCaml and store them in a list. I have tried to read with stdlib but it does not work. Also I cannot understand how scanf works for files. If someone could help me with scanf i would be grateful.

EditSorry for not being clear enough, first time posting. The input format is a b c\nd e f\n...Yes it is for my university. Basically i have to find mst of a graph. The input is vertex_1 vertex_2 weight \n and so on and i am trying to build a list of tuples [(vertex_1 vertex_2 weight),...] from input. In my code i am trying to gather chars to a string if its needed (ex two digit numbers) and then converting string to int. But i hope there is an easier way of doing that. I have to say that is the second day that i am programming in ocaml.

let entry_of_channel ch =  let number = input_char ch in   numberlet rec list_of_channel ch =  try    let e = entry_of_channel ch in    e:: list_of_channel ch  with    End_of_file -> []let string_of_chars chars =  let buf = Buffer.create 16 in  List.iter (Buffer.add_char buf) charslet rec list_clear list buffer =  match list with    [] -> []  | ''::t -> (string_of_chars buffer)::list_clear t []  | '\n'::t -> (string_of_chars buffer)::list_clear t []  | h::t ->      buffer @ h;    list_clear t bufferlet graph filename =   let ch = open_in filename in   let l = list_of_channel ch in   close_in ch;   let l_new = list_clear l [] in   l_new

Viewing all articles
Browse latest Browse all 527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>