Abhimanyu
Abhimanyu

Reputation: 4917

Erlang File Append mode

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

Upvotes: 11

Views: 6292

Answers (3)

Alan Moore
Alan Moore

Reputation: 1823

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

Upvotes: 10

Bas Bossink
Bas Bossink

Reputation: 9678

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.

Upvotes: 18

pgs
pgs

Reputation: 13955

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.

Upvotes: 7

Related Questions