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%

Unit 3 — File Handling

Lesson 17 of 32 in the free C Language notes on Siksha Sarovar, written by Rohit Jangra.

File Handling in C

File handling allows C programs to read from and write to files on disk, enabling permanent data storage.

---

File Pointer

All file operations use a FILE pointer:

FILE *fp;

---

Opening and Closing Files

FILE *fp = fopen("filename.txt", "mode");

if (fp == NULL) {
    printf("Error opening file!\n");
    return 1;
}

fclose(fp);

File opening modes:

ModeMeaning
"r"Read (file must exist)
"w"Write (creates/overwrites)
"a"Append (creates/appends)
"r+"Read + write
"w+"Read + write (overwrites)
"a+"Read + append
"rb", "wb"Binary read/write

---

Writing to a File

#include <stdio.h>

int main() {
    FILE *fp = fopen("output.txt", "w");
    if (fp == NULL) return 1;
    
    fprintf(fp, "Hello, File!\n");
    fputs("Another line\n", fp);
    fputc('A', fp);
    
    fclose(fp);
    return 0;
}

---

Reading from a File

#include <stdio.h>

int main() {
    FILE *fp = fopen("output.txt", "r");
    if (fp == NULL) return 1;
    
    char line[100];
    while (fgets(line, sizeof(line), fp) != NULL) {
        printf("%s", line);
    }
    
    fclose(fp);
    return 0;
}

---

File I/O Functions

FunctionPurpose
fopen()Open a file
fclose()Close a file
fprintf()Write formatted data
fscanf()Read formatted data
fgets()Read a line
fputs()Write a string
fgetc()Read a character
fputc()Write a character
feof()Check end of file
rewind()Move to start of file
fseek()Move to a position
ftell()Get current position

---

Binary File Example

struct Student { int id; char name[20]; float marks; };

/* Write binary */
FILE *fp = fopen("students.dat", "wb");
struct Student s = {1, "Alice", 90.5};
fwrite(&s, sizeof(s), 1, fp);
fclose(fp);

/* Read binary */
fp = fopen("students.dat", "rb");
struct Student t;
fread(&t, sizeof(t), 1, fp);
printf("%d %s %.1f\n", t.id, t.name, t.marks);
fclose(fp);