Erlang File Append mode

流过昼夜 提交于 2019-12-06 20:53:45

问题


I m trying to write some content in file using append mode in erlang but it giving error as bad argument.

Syntax used: file:write_file("/tmp/test1.txt","Abhimanyu","append").
error:{error,badarg}

thank you


回答1:


On the "don't create it if it doesn't exist" additional question, you have to be more creative by using something like file:read_file_info :

 case file:read_file_info(FileName) of
        {ok, FileInfo} ->
                 file:write_file(FileName, "Abhimanyu", [append]);
        {error, enoent} ->
                 % File doesn't exist
                 donothing
 end.

The append mode (or write mode) will create the file if it doesn't exist...




回答2:


The file:write_file function expects the last argument to be a list of atoms iso a string so changing your implementation to file:write_file("/tmp/test1.txt","Abhimanyu", [append]). should resolve your issue. Further examples can be found at TrapExit.




回答3:


I believe you need:

file:write_file("/tmp/test1.txt", "Abhimanyu", [append]).

I think you may also need to convert your data to a binary.



来源:https://stackoverflow.com/questions/1067617/erlang-file-append-mode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!