Lesson 25 of 50
cFile I/O (`stdio.h`)
10 mins
Course progress50%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 25 of 50
c10 mins
Keep these ideas in mind as you work through the lesson.
Files are accessed through FILE* streams.
FILE *fp = fopen("data.txt", "r");
if (!fp) { perror("open"); exit(EXIT_FAILURE); }
char line[128];
while (fgets(line, sizeof line, fp)) {
// process line
}
fclose(fp);
Modes:
r – read (file must exist).w – write (creates/overwrites).a – append.r+, w+, a+ – read/write variants.Always check the return of fread, fwrite, fprintf, and close the file with fclose.