【问题标题】:Erlang error: no function clause matching io:requestErlang 错误:没有匹配 io:request 的函数子句
【发布时间】:2016-10-06 10:14:27
【问题描述】:

我是一名经验丰富的 Erlang 程序员新手,但我坚持以下几点:

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

当从 shell 调用 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)

任何帮助将不胜感激。

【问题讨论】:

    标签: erlang


    【解决方案1】:

    两件事:

    "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.
    

    【讨论】:

      猜你喜欢
      • 2021-04-03
      • 2015-01-14
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      • 2018-11-29
      • 1970-01-01
      • 1970-01-01
      • 2017-11-30
      相关资源
      最近更新 更多