Opening a file with w+ lets the program write content, rewind, and read it back.

Write And Read

write_read.c
#include <stdio.h>

int main(void) {
    int count = ;
    FILE *file = fopen("write_read_demo.txt", "w+");

    if (file == 0) {
        return 1;
    }

    for (int i = 1; i <= count; i++) {
        fprintf(file, "%d\n", i);
    }

    rewind(file);

    int lines = 0;
    int value = 0;
    while (fscanf(file, "%d", &value) == 1) {
        lines++;
    }

    fclose(file);
    remove("write_read_demo.txt");

    printf("lines=%d\n", lines);
    return 0;
}
#include <stdio.h>

int main(void) {
    int count = ;
    FILE *file = fopen("write_read_demo.txt", "w+");

    if (file == 0) {
        return 1;
    }

    for (int i = 1; i <= count; i++) {
        fprintf(file, "%d\n", i);
    }

    rewind(file);

    int lines = 0;
    int value = 0;
    while (fscanf(file, "%d", &value) == 1) {
        lines++;
    }

    fclose(file);
    remove("write_read_demo.txt");

    printf("lines=%d\n", lines);
    return 0;
}
#include <stdio.h>

int main(void) {
    int count = ;
    FILE *file = fopen("write_read_demo.txt", "w+");

    if (file == 0) {
        return 1;
    }

    for (int i = 1; i <= count; i++) {
        fprintf(file, "%d\n", i);
    }

    rewind(file);

    int lines = 0;
    int value = 0;
    while (fscanf(file, "%d", &value) == 1) {
        lines++;
    }

    fclose(file);
    remove("write_read_demo.txt");

    printf("lines=%d\n", lines);
    return 0;
}
write mode `w+` creates a writable and readable file for this small example.
rewind After writing, `rewind` moves the stream position back to the beginning.