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
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

void print_first_character() {
    FILE* file = fopen("e.txt", "r");

    char ch = fgetc(file);  // Read one character
    printf("%c\n", ch);

    // CLOSE the file - You must close the file when you are done using it.
    fclose(file);
}

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

    // USE THIS FORM - DO NOT USE ANYTHING THAT INVOLVES EOF
    for(char ch = fgetc(file); !feof(file); ch = fgetc(file)) {
        printf("%c", ch);
    }

    // CLOSE the file - You must close the file when you are done using it.
    fclose(file);
    
    return EXIT_SUCCESS;
}
// fopen(filename, mode)
//  - mode can be "r" to read the file
//  - mode can be "w" to write to the file (truncates if the file exists)
//  - mode can be "a" to append to the end of the file

/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

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