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: Pietro Abate <Pietro.Abate@a...>
Subject: Re: [Caml-list] Better option to read a file

what's about ....

let file_ch = (open_in input_file) in
let read_lines =
    let read_new_line n =
        try Some (input_line file_ch)
        with End_of_file -> None
    in
        Stream.from read_new_line
in
let rec get_line () = 
    match Stream.next read_lines with
    |s when Str.string_match (Str.regexp "^[\n\t ]+$") s 0 -> get_line ()
    |s -> s
in

p

On Tue, Mar 16, 2004 at 10:28:37PM +0100, Agust?n Valverde wrote:
> 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

-- 
++ If you have an apple and I have an apple and we  exchange apples 
   then you and I will still each have one apple. But if you have 
   an idea and I have an idea and we exchange these ideas, then each 
   of us will have two ideas. -- George Bernard Shaw
++ Please avoid sending me Word or PowerPoint attachments.
   See http://www.fsf.org/philosophy/no-word-attachments.html

-------------------
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