Tutorials

C Tutorial

All Tutorials Learning Tools Login

C Tutorial · Chapter 15 of 16

C File Handling

Create, write, read, update, and delete text files.

fopen opens a file. fprintf writes. fscanf/fgets reads. fclose closes. remove deletes.

Always check whether the file opened successfully before using it.

File sample Try it Yourself »
#include <stdio.h>

int main(void) {
    FILE *file = fopen("note.txt", "w");
    if (file == NULL) {
        printf("Cannot create file.\n");
        return 1;
    }
    fprintf(file, "Saved from C Tutorial\n");
    fclose(file);
    printf("File saved.\n");
    return 0;
}

Classroom Tips

  • Close files when finished.
  • Use Practice Mode File Handling drills next.