ocaml_如何在OCaml中将行打印到STDERR和STDOUT?

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!
Answered by dtivl.
dtivl回答。

翻译自: https://www.systutorials.com/how-to-print-a-line-to-stderr-and-stdout-in-ocaml/

ocaml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值