I am trying to print the contents of a file to a new file line by line. However After running this function, only the last line of the input file is written to the output file.
let in_channel = open_in inFile in
try
while true do
let line = input_line in_channel in
let oc = open_out outFile in
fprintf oc "%s\n" line;
close_out oc;
done
with End_of_file ->
close_in in_channel
What am I doing wrong? I am inputting the newline in the printf statement, so I am very confused.
http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#VALopen_out:
val open_out : string -> out_channel
Open the named file for writing, and return a new output channel on that file, positioned at the beginning of the file. The file is truncated to zero length if it already exists. It is created if it does not already exists.
You're calling open_out
repeatedly inside the loop. For every line you read, you first truncate outFile
back to length 0 before writing to it.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments