我有以下代码:
file:write(FileId, Packet),
file:close(FileId),
{ok, FileId1} = file:open("tmp/" ++ integer_to_list(Summ), [read]),
A = file:read_file("tmp/" ++ integer_to_list(Summ)),
{_, B} = A,
io:format(binary_to_list(B));
在功能结束时,我看到io:format
B输出。但如果我这样做:
file:write(FileId, Packet),
file:close(FileId),
{ok, FileId1} = file:open("tmp/" ++ integer_to_list(Summ), [read]),
A = file:read_file("tmp/" ++ integer_to_list(Summ)),
{_, B} = A,
S = binary_to_list(B),
io:format(S);
io:format nothing output. Why?
谢谢。
答案 0 :(得分:4)
您可能不应该依赖单参数io:format/1
函数:
3> B="helo\n".
"helo\n"
4> io:format(B).
helo
ok
5> C="~p".
"~p"
6> io:format(C).
** exception exit: {badarg,[{io,format,[<0.26.0>,"~p",[]]},
{erl_eval,do_apply,5},
{shell,exprs,6},
{shell,eval_exprs,6},
{shell,eval_loop,3}]}
in function io:o_request/3
如果参数包含任何格式请求,则io:format
调用可能会死亡。我建议使用格式字符串,例如~p~n
or ~w~n
:
1> C="~p".
"~p"
2> io:format("~p~n", [C]).
"~p"
ok
答案 1 :(得分:1)
它在这方面做得非常好:
3> {ok, FileId1} = file:open("tmp/" ++ integer_to_list(Summ), [read]),
3> A = file:read_file("tmp/" ++ integer_to_list(Summ)),
3> {_, B} = A,
3> S = binary_to_list(B),
3> io:format(S).
1213
答案 2 :(得分:0)
io:format/1
的参数是格式字符串,它不能包含任何需要参数的格式化控制序列。如果发生这种情况,则会生成错误。要输出原始可打印字符,请使用io:put_chars/1
。