Charlie-Greenman
Charlie-Greenman

Reputation: 1659

How to read a file, and write it to a different file

I hope all is well. At this point, I plan on reading a file, and then writing it to another file. (The equivalent of cp in OCaml, before I go ahead and manipulate text.) Currently in my code, I am making using the extlib to read a file, and then output it. I am also using dune to build file as an executable file. My file looks something like the following:

(* example.ml file *) 
let read_filename = "example_1.ts"
let filename = "example_2.ts"

let () =

  let text read_filename =
    let chan = open_in read_filename in
    Std.input_list chan  

  let filename = filename in 
  let text = text in 
  Std.output_file ~filename ~text 
  (* normal exit: all channels are flushed and closed *)

When I build the file using dune build example.bc I receive the following error:

File "example.ml", line 11, characters 2-5:
Error: Syntax error

I am trying to figure out what I am doing wrong, but to no avail. Any help would be more than appreciated. Thank you.

Upvotes: 0

Views: 110

Answers (1)

Et7f3XIV
Et7f3XIV

Reputation: 619

When I have readed you code I remark these lines.

  let filename = filename in 
  let text = text in 

Why have you writting this line ? (* You could delete it safely *)

And then I have found your syntax error was a line above you have forgotten a in

(* example.ml file *) 
let read_filename = "example_1.ts"
let filename = "example_2.ts"

let () =
  let text read_filename =
    let chan = open_in read_filename in
    Std.input_list chan
  in
    Std.output_file ~filename ~text 
  (* normal exit: all channels are flushed and closed *)

The rule for each local definition you must have a in keyword (* a local definition is a let inside a let expression *)

Upvotes: 1

Related Questions