C Workshop · Write a Line
Lesson C

Write a Line

Open a file, write one line, close it. Practice prints OK to show the steps succeeded.

What it is

Write mode creates a file or wipes an old one, then you put text in it. The visualizer shows Hello inside notes.txt. The program also prints OK on the terminal to say the steps succeeded.

Approach

  1. fopen(..., "w").
  2. fprintf one line.
  3. fclose, then printf OK.

Where this is used in the real world

Diagram

1

w

create

2

Hello

write

3

close

save

4

OK

stdout

Run algorithm

Each step highlights the matching C line. Program output appears only when a line that prints actually runs.

Ready
C code
1#include <stdio.h>
2int main(void) {
3    FILE *f = fopen("notes.txt", "w");
4    if (!f) return 1;
5    fprintf(f, "Hello\n");
6    fclose(f);
7    printf("OK\n");
8    return 0;
9}
What this line does
Press Run algorithm. The highlight will move through the C code.
file is closed
Program output
(no output yet)

Check your understanding

Five questions. Pick one answer each, then Check answers. The correct choice turns green.

1. Stdout in this lesson is:

printf OK.

2. The file contents after fprintf are:

Visualizer file card.

3. "w" on an existing file:

Wipe.

4. Another kind "a" would:

Append.

5. Practice may only printf OK because:

Sandbox vs diagram.