Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. C Programming Fundamentals

Lesson 25 of 50 · c

File I/O (`stdio.h`)

Duration: 10 mins

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.

Previous: Standard Input / Output (`stdio.h`)Next: Preprocessor Directives