2016-10-06 33 views
0

我是一個經驗豐富的二郎神程序員新我被困在以下方面:二郎錯誤:沒有功能的語句匹配的IO:請求

myread() -> 
    {_, MyData } = file:read_file("hands.txt"), 
    io:format("hands-out.txt", "~w", MyData). 

收益率,當myread()從外殼調用:

** exception error: no function clause matching io:request("hands-out.txt", 
      {format,"~w", <<"3h 5h 7h 8h 3h 5h 7h 8h q"...>>}) 
     (io.erl, line 556) in function io:o_request/3 (io.erl, line 63) 

任何幫助,將不勝感激。

回答

4

兩件事情:

"hands-out.txt", "~w"必須是一個字符串:"hands-out.txt: ~w"

而這在更換~w需要是列表中的數據。所以:

io:format("hands-out.txt: ~w", [MyData]).

http://erlang.org/doc/man/io.html#format-2

另外,你應該從file:read_file/1返回的狀態值模式匹配。在您的版本中,由於您使用的是_,因此會返回{error, Reason}的錯誤,您將打印錯誤原因而不是文件,這可能會造成混淆。

因此,要麼讓它{ok, MyData } = file:read_file("hands.txt")如果你想如果你要處理這種情況的讀取錯誤,或類似以下崩潰:

myread() -> 
    case file:read_file("hands.txt") of 
    {ok, MyData } -> 
     io:format("hands-out.txt: ~w", [MyData]); 
    {error, Error} -> 
     io:format("Error: ~w~n", [Error]) 
    end.