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