The file module (part 2). (Learning Erlang 8)

Published on Oct 11, 2012

Other articles in the Learning Erlang series.

  1. Learning Erlang.
  2. Heads and tails, working with lists. (Learning Erlang 2)
  3. Variables, comparisons and dynamic typing. (Learning Erlang 3)
  4. Modules and functions, Erlang building blocks (Learning Erlang 4).
  5. Module attributes and compilation (Learning Erlang 5).
  6. Playing with recursion, (Learning Erlang 6).
  7. Playing with the file module (part 1). (Learning Erlang 7)
  8. String manipulation in Erlang. (Learning Erlang 9)
  9. Records. (Learning Erlang 10)
  10. Functional arrays. (Learning Erlang 11)

Let’s start by creating a file.

We can use the open/2 function with append or write as the mode.

  1> {Result, Device} = file:open("/tmp/new", [append]).
  {ok, <0.50.0>}
  2> {Result2, Device2} = file:open("/tmp/2", [write]).
  {ok, <0.34.0>}

The Device and Device2 variables contain an IoDevice that can be a process id or a file descriptor, in this case is the process id that opened the file.

Adding some data.

We can use write/2 to add some data to a file we opened in append or write mode.

  3> file:write(Device, "Some data.").
  4> file:write(Device, "Some more data.").
  5> file:write(Device, "And some more...").

Cleaning after ourselves.

Closing the file passing the pid.

  6> file:close(Device).

How about create the file and add some data to it all in one line?

write_file/2 takes the file path and the data to write. It will create a file if it doesn’t exist or override the content of the given file.

  1> file:write_file("/tmp/2","Some data").

If the file exists and we don’t want to override the content, we use write_file/3 instead, using the desired mode as the third argument.

  2> file:write_file("/tmp/2","\n\rAnother line", [append]).

Or deleting an existing file.

  3> file:delete("/tmp/2").

Remove the content of a file.

We can call truncate with a IoDevice to remove all content.

  4> file:truncate(Device).

Rename a file

This one works similar to the mv command. It can take paths to the files or IoDevice as both the source and the destination for the file.

  5> file:rename("/tmp/3","/tmp/renamed-3").

Writing is cool but let’s do some reading.

  6> file:read_file("/tmp/4").
  {ok,<<"Data hereOverriding the file content.">>}

You can easily read the content of a file using the read_file/1 function.

If you opened the file with open/2 you should use read/2 instead.

  7> {Status, IOD} = file:open("/tmp/4", [read]).
  8> file:read(IOD, 20).
  {ok,"Data hereOverriding "}
  9> file:read(IOD, 0).
  10> file:read(IOD, 200).
  {ok,"the file content."}
  11> file:read(IOD, 20000).

The second argument is the number of bytes/characters to read.

If you try to read passing the end of the file you get an eof error.

Also notice that if you read 0 characters you get an empty list as a result.

Remember, strings are nothing more than lists of characters in Erlang.

Getting info about a file.

You can use read_file_info/1 calling with a file name or read_file_info/2 calling it with the file name and some file_info_options.

  1> file:read_file_info("/tmp/4").
  {ok, {file_info,37,regular,read_write,
               { {2012,10,12},{0,25,17} },
               { {2012,10,12},{0,18,44} },
               { {2012,10,12},{0,18,44} },
               33204,1,2049,0,3167603,1000,1000} }

There is more.

The file module has a lot more to offer. You can copy files, create links, create and delete directories, change date and time file information for the file, change ownership and more.

Note of caution. These are my notes while learning Erlang. You are welcome to follow along and use them as a guide. Please make sure to check the Erlang language site