<aside> <img src="/icons/home_green.svg" alt="/icons/home_green.svg" width="40px" />

Back to Home

</aside>

https://jennychen9827sbu.github.io/CAD281/filescan.html


Sample Questions with Source Code

Flight Schedule

ESE 124 Grader Filter

Edit 2D Character Array

Student Record Processor

Advanced Identifier Scanner

Token Analyzer

๐Ÿงพ Reading Text Files in C โ€“ fscanf, fgets, fgetc

What are these?

Function Purpose Reads by...
fscanf() Reads formatted input (like scanf) words or numbers
fgets() Reads one full line as a string entire line
fgetc() Reads one character at a time character

1. fscanf โ€“ Read numbers or words

Sample File: numbers.txt

10 20 30
40 50

Code: read_fscanf.c

#include <stdio.h>

int main() {
    FILE* fp = fopen("numbers.txt", "r");
    int num;

    if (!fp) {
        printf("File not found.\n");
        return 1;
    }

    while (fscanf(fp, "%d", &num) != EOF) {
        printf("Number: %d\n", num);
    }

    fclose(fp);
    return 0;
}


2. fgets โ€“ Read full lines (good for strings)

Sample File: lines.txt

Hello, ESE 124!
C programming is fun.
File reading line by line.

Code: read_fgets.c