Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Better option to read a file
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Agustín_Valverde <a_valverde@c...>
Subject: [Caml-list] Better option to read a file
Hi

In a program  I need to read the input data from a file and I have 
written several options. I want to obtain the string of characters from 
a text file and I don't know what is the better option. Among others, I 
have written the following:

First option:

let leer_file fl =
      let form = ref "" in
      let arch = open_in fl in
      let long = in_channel_length arch in
      form := String.create (long-1);
      really_input arch (!form) 0 (long-1);
      close_in arch;
      !form;;

Second:

let rec unir c ac = unir ac^(Char.escaped c);;

let leer2 fl =
      let form = ref "" in
      let c = ref '-' in
      let arch = open_in fl in
      (try
        (while true do (c := input_char arch); (if !c != '\n' then (form 
:= unir !c !form) else ()) done)
      with End_of_file -> close_in arch);
      !form;;

I also have a parser to convert the string, could I to improve these 
functions merging them with the parser in some way?

Thanks for your help

Agustín Valverde
Department of Applied Mathematics
University of Malaga, Spain

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners