Skip to main content

File Handling in C

File handling allows you to store and access data permanently on disk. Unlike variables (which lose their values when the program ends), a file retains its contents even after the program closes.

Steps in File Handling

  1. Declare a file pointer
  2. Open the file
  3. Perform read/write operations
  4. Close the file

File Opening Modes


Common File Functions


Writing to a File


Reading from a File


Appending to a File


Checking for NULL

Always check if fopen() returned NULL — it means the file could not be opened (wrong path, no permissions, etc.):