Erlang basics · Lesson 19 (open contents)
01 · Open erl02 · Meet common terms03 · Tell two kinds of text apart04 · Bind a variable once05 · Split and filter a list06 · Parse a number safely07 · Choose one path with case08 · Let a function keep going09 · Put code in a module10 · Compare terms and Boolean results11 · Read and update nested maps12 · Build and inspect UTF-8 binaries13 · Let clauses choose the function path14 · Solve a list twice15 · Choose with patterns, guards, and errors16 · Describe a module and one record17 · Put one tested module in Rebar318 · Return success or failure as data19 · Read and write one file safely20 · Build a data pipeline with functions21 · Keep records behind a module boundary22 · Define a module contract23 · Describe the data contract24 · Test small rules and whole flows25 · Shape builds and make a command26 · Write the project promise27 · Parse one real line28 · Keep the public API small29 · Make the promise executable30 · Package the edge and leave clues31 · Prove the project is done
ERLANG · INTERMEDIATE · LESSON 1925 minutes

Read and write one file safely

Build a path, write bytes, and match the result of reading them back.

01 · Start with the whole example

Run this first

Run it once, change one input, and compare the new result.

Erlang
%% Keep the path and both IO results visible
Path = filename:join(["tmp", "note.txt"]),
ok = filelib:ensure_dir(Path),
ok = file:write_file(Path, <<"ready\n">>),
file:read_file(Path).
Check the result first
  • The final call returns {ok,<<"ready\n">>} when the file is readable.
02 · Take the code apart

Read from the first line down

  1. Read filename:join/1: Join path pieces for the current operating system.
  2. Read file:write_file/2: Write iodata to a file.
  3. Read file:read_file/1: Read a whole file as a binary result.
03 · Meet the new symbols

Symbols are not secret signs

filename:join/1

Join path pieces for the current operating system.

file:write_file/2

Write iodata to a file.

file:read_file/1

Read a whole file as a binary result.

04 · Ideas inside the code

Match each name to its meaning

01

path

A path names a place in the file system; filename:join/1 builds it without hand-written separators.

02

binary file data

file:read_file/1 returns file contents as a binary when it succeeds.

03

IO result

File functions return {ok, Value}, ok, or {error, Reason} instead of hiding failure.

05 · Make the idea clear

Why these forms are useful

Run the example first. Predict one result, then change one input and run it again.

A path names a place in the file system; filename:join/1 builds it without hand-written separators.

file:read_file/1 returns file contents as a binary when it succeeds.

06 · Change it yourself

Close the answer and try

Match a missing-file error without crashing.

Practice starting point
case file:read_file("missing.txt") of
  {ok, Data} -> {ok, Data};
  {error, ____} -> missing
end.

Target result: Bind the reason to enoent.

Stuck? Read one hint

The common missing-file reason is the atom enoent.

After you run it, see one answer
One answer
case file:read_file("missing.txt") of
  {ok, Data} -> {ok, Data};
  {error, enoent} -> missing
end.
Think about it: Why use `filename:join/1`?

It keeps path construction readable and portable across operating systems.

Take with you

Remember these three lines

  1. 1

    Build paths with filename.

  2. 2

    Match file results.

  3. 3

    Keep external IO at a clear boundary.

Lesson completeRun the code once, then mark this lesson as done.