From: Markus M. <ma...@oe...> - 2003-10-05 11:11:14
|
Hi, one function that people may want to have in the standard library is "read_file", which opens a file with a given name and returns its complete contents as a string: --------------------------------------------------------------------------- let read_file name = let file = open_in name in let size = in_channel_length file in let buf = String.create size in begin try really_input file buf 0 size with exc -> begin try close_in file with _ -> () end; raise exc end; close_in file; buf --------------------------------------------------------------------------- This functions is optimal for files, because it uses the length of the file to determine the required buffer size. Note the following behaviour: if reading causes an exception, the program will attempt to close the opened file. If this also causes an exception, the FIRST exception will be reraised. I think this is the most reasonable way to handle this. Regards, Markus -- Markus Mottl http://www.oefai.at/~markus ma...@oe... |