Erlang - write



This method is used to write the contents to a file.

Syntax

write(FileHandler,text)

Parameters

  • FileHandler − This is the handle to a file. This handle is the one that would be returned when the file:openoperation is used.

  • Text − The text which needs to be added to the file.

Return Value

None

For example

-module(helloworld). 
-export([start/0]). 

start() -> 
   {ok, Fd} = file:open("Newfile.txt", [write]), 
   file:write(Fd,"New Line").

Output

Whenever the above code is run, the line “New Line” will be written to the file. Note that because the mode is set to write, if there were any previous contents to the file, they will be overwritten.

To append to the existing contents of the file, you need to change the mode to append as shown in the following program.

-module(helloworld). 
-export([start/0]). 

start() -> 
   {ok, Fd} = file:open("Newfile.txt", [append]), 
   file:write(Fd,"New Line").
erlang_file_input_output.htm
Advertisements