ocaml
In OCaml, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely?
在OCaml中 ,如何将字符串打印为一行到STDOUT ? 也就是说,字符串和换行符很好吗?
And similarly, how to print the line to STDERR?
同样,如何将行打印到STDERR?
In OCaml, you may print a string with the end line character to STDOUT using the function in the Pervasives module:
在OCaml中 ,您可以使用Pervasives模块中的函数将带有结束行字符的字符串打印到STDOUT:
val print_endline : string -> unit
Print a string, followed by a newline character, on standard output and flush standard output.
在标准输出和刷新标准输出上打印字符串,后跟换行符。
One usage example:
一个用法示例:
$ ocaml
OCaml version 4.01.1+dev2-2013-12-18+CLOSED
# print_endline "hello world!";;
hello world!
- : unit = ()
#
In OCaml, you can use the Printf.eprintf
function to print to STDERR (which is in Pervasives.stderr
).
在OCaml中 ,可以使用Printf.eprintf
函数打印到STDERR(位于Pervasives.stderr
)。
Examples:
例子:
Use Printf.eprintf
:
使用Printf.eprintf
:
$ rlwrap ocaml 2>/tmp/a.txt
OCaml version 4.01.1+dev2-2013-12-18+CLOSED
Cannot find file topfind.
Unknown directive `thread'.
# Printf.eprintf "hello world!";;
- : unit = ()
#
$ cat /tmp/a.txt
hello world!
Use Printf.fprintf
to print to Pervasives.stderr
:
使用Printf.fprintf
打印到Pervasives.stderr
:
$ rlwrap ocaml 2>/tmp/a.txt
OCaml version 4.01.1+dev2-2013-12-18+CLOSED
Cannot find file topfind.
Unknown directive `thread'.
# Printf.fprintf Pervasives.stderr "hello world!";;
- : unit = ()
#
$ cat /tmp/a.txt
hello world!
翻译自: https://www.systutorials.com/how-to-print-a-line-to-stderr-and-stdout-in-ocaml/
ocaml