C Workshop · Append a Line
Lesson C

Append a Line

Mode "a" adds at the end instead of wiping the file. Old text stays.

What it is

Append mode adds at the end. Old text stays. Hello was already in the file; after this run the file has Hello and then C. The terminal still prints OK.

Approach

  1. fopen(..., "a").
  2. fprintf the new line.
  3. fclose and print OK.

Where this is used in the real world

Diagram

1

Hello

old

2

a

append

3

C

new

4

two lines

file

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", "a");
4    if (!f) return 1;
5    fprintf(f, "C\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. Starting file Hello, after append C the file has:

Old + new.

2. Stdout is:

printf OK.

3. If you had used "w" instead:

Overwrite.

4. Append mode is:

a.

5. Another kind: logging without fclose every line might:

Buffering.