1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

// FILES - how to read a file one character at a time
//
// CAUTION: Googling this may make your life harder.
// CAUTION: Using EOF is strongly discouraged as it tends to be bug-prone.
//
// char has range -128 to 127 and is usually used for human-readable text.
// unsigned char has range 0 to 255 and can include binary bytes and unprintable stuff

int main(int argc, char* argv[]) {
    FILE* fp = fopen("1.read_file.txt", "r");

    // READ CHARACTERS ONE BY ONE
    for(unsigned char ch = fgetc(fp); !feof(fp); ch = fgetc(fp)) {
        fputc(ch, stdout);
    }

    fclose(fp); // Always close file when you are done with it.

    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2022 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.