【问题标题】:Issues with reading file in erlang在erlang中读取文件的问题
【发布时间】:2018-12-03 05:17:35
【问题描述】:

所以,我正在尝试读取和写入文件。 在写入文件时,我需要检查文件中是否存在特定索引,然后我不会写入并抛出错误。 文件中的数据如下所示:

{1,{data,dcA,1}}.
{2, {data, dcA, 2}}.
{3,{data,dcA,3}}.

我在每一行的末尾添加了点,因为 file:consult() 需要这样的文件。

就是这种格式。

{Index, {Data, Node, Index}}

当我必须添加一个新文件时,我会检查这个索引。

这是我迄今为止尝试过的 - https://pastebin.com/apnWLk45

我是这样运行的:

193> {ok, P9} = poc:start(test1, self()).
{ok,<0.2863.0>}
194> poc:add(P9, Node, {6, data}).

在 poc:add/3 中,P9 是来自 file:open 的进程 ID。 我之前在 shell 中定义为 dcA 第三个是数据——采用这种格式——{Index, data}

由于我使用的是 file:consult/1,它以文件名作为参数。那时,我只有进程 ID。所以我取这个名字 file:pid2name(_Server).

当我第一次运行它时,它运行得很好。

当我再次运行它时 - poc:add(P9, Node, {6, data2}),我在这行 file:pid2name(_Server) 中收到一个错误。

exception error: no match of right hand side value undefined

我该如何解决这个问题?

我是 Erlang 的新手。刚开始学习一周。

【问题讨论】:

    标签: functional-programming erlang


    【解决方案1】:

    我正在尝试读取和写入文件。在写入时 文件,我需要检查文件中是否存在特定索引,然后我不 写入并抛出错误。

    DETS 表可以轻松完成您想要的操作:

    -module(my).
    -compile(export_all).
    
    open_table() ->
        dets:open_file(my_data, [{type, set}, {file, "./my_data.dets"}]).
    
    close_table() ->
        dets:close(my_data).
    
    clear_table() ->
        dets:delete_all_objects(my_data).
    
    insert({Key, _Rest}=Data) ->
        case dets:member(my_data, Key) of 
            true    -> throw(index_already_exists);
            false   -> dets:insert(my_data, Data)
        end.
    
    all_items() ->
        dets:match(my_data, '$1').
    

    在外壳中:

    ~/erlang_programs$ erl
    Erlang/OTP 20 [erts-9.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
    
    Eshell V9.2  (abort with ^G)
    
    1> c(my).
    my.erl:2: Warning: export_all flag enabled - all functions will be exported
    {ok,my}
    
    2> my:open_table().
    {ok,my_data}
    
    3> my:clear_table().
    ok
    
    4> my:all_items().
    []
    
    5> my:insert({1, {data, a, b}}).
    ok
    
    6> my:insert({2, {data, c, d}}).
    ok
    
    7> my:insert({3, {data, e, f}}).
    ok
    
    8> my:all_items(). 
    [[{1,{data,a,b}}],[{2,{data,c,d}}],[{3,{data,e,f}}]]
    
    9> my:insert({1, {data, e, f}}).
    ** exception throw: index_already_exists
        in function  my:insert/1 (my.erl, line 15)
    

    当我再次运行时 - poc:add(P9, Node, {6, data2}),我得到一个错误 在这一行文件中:pid2name(_Server):

    exception error: no match of right hand side value undefined
    

    当进程打开文件时,它会链接到处理文件 I/O 的进程,这意味着如果打开文件的进程异常终止,I/O 进程也将终止。这是一个例子:

    -module(my).
    -compile(export_all).
    
    start() ->
        {ok, Pid} = file:open('data.txt', [read, write]),
        spawn(my, add, [Pid, x, y]),
        exit("bye").
    
    
    add(Pid, _X, _Y) ->
        timer:sleep(1000),  %Let start() process terminate.
        {ok, Fname} = file:pid2name(Pid),
        io:format("~s~n", [Fname]).
    

    在外壳中:

    1> c(my).     
    my.erl:2: Warning: export_all flag enabled - all functions will be exported
    {ok,my}
    
    2> my:start().
    ** exception exit: "bye"
         in function  my:start/0 (my.erl, line 7)
    3> 
    =ERROR REPORT==== 25-Jun-2018::13:28:48 ===
    Error in process <0.72.0> with exit value:
    {{badmatch,undefined},[{my,add,3,[{file,"my.erl"},{line,12}]}]}
    

    根据pid2name() docs

    pid2name(Pid) -> {ok, Filename} | undefined
    

    该函数可以返回undefined,这就是错误消息所说的发生了。

    【讨论】:

      猜你喜欢
      • 2016-07-02
      • 1970-01-01
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多