tags:

views:

318

answers:

3

Hi, 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  A: 

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.

pgs
+7  A: 

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.

Bas Bossink
Thanx for the help...one more thing i wanna know it wont create the file ..if it dodesnt exists.
Abhimanyu
+4  A: 

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

Alan Moore