Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

File Handling: Permanent Data Storage

Lesson 29 of 53 in the free Foundation of C & C++ notes on Siksha Sarovar, written by Rohit Jangra.

Beyond the Lifespan of a Program

Variables and arrays only exist in RAM. As soon as your program finishes or the power goes out, that data is gone forever. Files allow you to save data permanently on the hard drive.

The FILE Pointer

In C, you don't interact with the hard drive directly. You use a special structure called FILE provided by <stdio.h>. You always work with a pointer to this structure.

FILE *fp;

The Core Lifecycle of a File

  1. Open: fp = fopen("data.txt", "w");
  2. Check: if (fp == NULL) - Always check if the file actually opened!
  3. Process: Use fprintf, fscanf, fputs, etc.
  4. Close: fclose(fp);. Critical: If you forget this, your data might not actually be saved to the disk.

Opening Modes

ModeDescriptionBehavior
"r"ReadOpens existing file. Fails if it doesn't exist.
"w"WriteCreates new file. Warning: Deletes everything in an existing file!
"a"AppendKeeps existing data and adds new data to the end.
"rb/wb"BinaryOpens for reading/writing non-text data (like images).

Reading and Writing Functions

  • fprintf(fp, ...): Just like printf, but sends the output to a file.
  • fscanf(fp, ...): Just like scanf, but reads from a file.
  • fputs(str, fp): Writes a string to the file.
  • fgets(str, size, fp): Reads a string safely from the file.

Random Access (fseek and ftell)

Normally, files are read sequentially from start to finish. But C allows you to "jump" to any specific byte in a file using fseek. This is how database software finds a specific record in a 100GB file without reading the whole thing.

Always close your files! The operating system has a limit on how many files a program can keep open at once. If you keep opening files without closing them, your program will eventually fail.