Version française
Home     About     Download     Resources     Contact us    
Browse thread
Trouble with Scanf and files
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Olivier Andrieu <oandrieu@n...>
Subject: Re: [Caml-list] Trouble with Scanf and files
Hi,

On Jan 18, 2008 1:51 AM, Christopher Kauffman <kauffman@cs.umn.edu> wrote:
> I have encountered some difficulty using Scanf with a large number of files. As
> suggested in the the Ocaml manual, my strategy so far when reading a number of
> files has been to use Scanf.Scanning.from_file to create a scanbuf for each
> file. I had always wondered if this would cause problems with large numbers of
> files because I could not figure out a way to close a file after completing
> operations on it. Today I attempted to process too many and received a fatal
> system error. If one allocates a scanbuf via
>
> let b = Scanf.Scanning.from_file "somefile" in ...
>
> is there a way to subsequently close the file when the buffer is no longer
> needed? Or is this an issue that should be taken care of by the garbage
> collector in some strange way?

The garbage collector does not close I/O channels.

Have a look at the implementation of Scanf:
http://camlcvs.inria.fr/cgi-bin/cvsweb/ocaml/stdlib/scanf.ml?rev=1.77
Apparently close_in is called when the end of file is encountered.
So if you stop scanning before the end of file, you'd better use
Scanning.from_channel and close the channel yourself.

-- 
  Olivier