Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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

On Jan 18, 2008 1:51 AM, Christopher Kauffman <> 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:
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.