Read entire file: Difference between revisions

Content deleted Content added
Line 228:
There is no problem reading an entire file with the function <code>really_input</code> because this function is implemented appropriately with an internal loop, but it can only load files which size is equal or inferior to the maximum length of an ocaml string. This maximum size is available with the variable <code>Sys.max_string_length</code>. On 32 bit machines this size is about 16Mo.
 
To load bigger files several solutions exist, for example create a structure that containcontains several strings where the contents of the file can be split. Or an another solution that is often used is to use a bigarray of chars instead of a string:
 
<lang ocaml>type big_string =
Line 245:
(bstr)</lang>
 
Then the length of the data can be get with <code>Bigarray.Array1.dim</code> instead of <code>String.length</code>, and we can access to a given char with the syntactic sugar <code>bstr.{i}</code> (instead of <code>str.[i]</code>) as showshown in the small piece of code below (similar to the cat command):
 
<lang ocaml>let () =