【发布时间】:2014-05-05 21:50:30
【问题描述】:
我有以下代码从文件中读取行
let read_file filename =
let lines = ref [] in
let chan = open_in filename in
try
while true do
lines := input_line chan :: !lines
done;
!lines
with End_of_file ->
close_in chan;
List.rev !lines
但是,此代码在字符“\n”中添加了额外的“\”。
例如。
helloworld\n
被读入
helloworld\\n
我应该如何关闭它以便获得helloworld\n?
非常感谢!
编辑
所以我用这个测试了:
let read_file filename =
let lines = ref [] in
let chan = open_in filename in
try
while true do
lines := input_line chan :: !lines
done;
!lines
with End_of_file ->
close_in chan;
List.rev !lines
let () =
let lines = read_file "test.txt" in
for i=1 to List.length lines
do
Printf.printf("%s") (List.nth lines (i-1))
done;
test.txt
helloworld\n
输出是helloworld\n,而不是helloworld,换行。
【问题讨论】: