<aside> <img src="/icons/home_green.svg" alt="/icons/home_green.svg" width="40px" />
</aside>
https://jennychen9827sbu.github.io/CAD281/filescan.html
fscanf, fgets, fgetc| 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 |
fscanf โ Read numbers or wordsnumbers.txt10 20 30
40 50
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;
}
fgets โ Read full lines (good for strings)lines.txtHello, ESE 124!
C programming is fun.
File reading line by line.
read_fgets.c