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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

// FILES - Same code but using while loop.
// 
// USE FOR LOOP INSTEAD.  This is just for learning.

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

    // READ CHARACTERS ONE BY ONE -- REMINDER:  You should use a for loop for this.
    unsigned char ch = fgetc(fp);   // «INIT»
    while(!feo(fp)) {   // «CONDITION» (!feof(fp))
        fputc(ch, stdout);
        ch = fgetc(fp);  // «NEXT»
    }
    // If the file were empty, then at line 14, ch would be set «don't worry about it»
    // and the EOF indicator on the file pointer would be set to true.  Then, feof(…)
    // would return true thereafter.
    //
    // Otherwise, line 14 will set ch to the first character.  Then, we print the
    // character on line 16.  Finally, we get the next character in the file at line 17.

    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.