πŸ“‚ File Handling in C

πŸ”Ή What is File Handling?

File handling in C allows a program to store data permanently on disk, read it, and modify it later.

  • Files are stored in secondary memory (hard disk)
  • Variables are stored in RAM (temporary)
  • Data remains available even after program ends

βœ… Why Use File Handling?

  • To save data permanently
  • To store large data for future use
  • Used in databases, logs, reports
  • Helpful in text and data processing

πŸ“ File Types in C

File Type Description Example
Text File Human-readable text .txt
Binary File Machine-readable data .dat, .bin

πŸͺœ Steps to Handle Files in C

  1. Declare a file pointer
  2. Open a file using fopen()
  3. Perform read/write operations
  4. Close the file using fclose()
FILE *fp;

1️⃣ Opening a File

FILE *fopen("filename", "mode");

πŸ“Œ File Modes in C

Mode Description
"r"Read (file must exist)
"w"Write (overwrite/create)
"a"Append (write at end)
"r+"Read + Write
"w+"Write + Read
"a+"Append + Read

2️⃣ Writing to a File (fprintf)

#include <stdio.h>

int main() {
  FILE *fp;
  fp = fopen("data.txt", "w");

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

  fprintf(fp, "Hello World\\n");
  fprintf(fp, "Welcome to C Programming\\n");

  fclose(fp);
  return 0;
}

This creates data.txt and writes data into it.

3️⃣ Reading from a File (fgets)

#include <stdio.h>

int main() {
  FILE *fp;
  char str[100];

  fp = fopen("data.txt", "r");
  if(fp == NULL) {
    printf("File not found");
    return 1;
  }

  while(fgets(str, 100, fp) != NULL) {
    printf("%s", str);
  }

  fclose(fp);
  return 0;
}

βœ” Use fgets() for line-by-line βœ” Use fgetc() for character-by-character

4️⃣ Appending Data to a File

#include <stdio.h>

int main() {
  FILE *fp;
  fp = fopen("data.txt", "a");

  fprintf(fp, "This line is appended.\\n");

  fclose(fp);
  return 0;
}

5️⃣ File Positioning Functions

Function Description
ftell(fp)Returns current position
fseek(fp, offset, whence)Moves file pointer
rewind(fp)Moves to beginning

6️⃣ Binary File Handling

✍ Writing to Binary File

#include <stdio.h>

struct Student {
  int roll;
  char name[20];
};

int main() {
  FILE *fp;
  struct Student s = {1, "Rahul"};

  fp = fopen("student.dat", "wb");
  fwrite(&s, sizeof(s), 1, fp);

  fclose(fp);
  return 0;
}

πŸ“– Reading from Binary File

#include <stdio.h>

struct Student {
  int roll;
  char name[20];
};

int main() {
  FILE *fp;
  struct Student s;

  fp = fopen("student.dat", "rb");
  fread(&s, sizeof(s), 1, fp);
  printf("%d %s", s.roll, s.name);

  fclose(fp);
  return 0;
}

πŸ“š FAQs on File Handling in C

Q1. What is a file?

A collection of data stored permanently on disk.

Q2. How do we open a file?

Using fopen("filename", "mode").

Q3. Difference between text and binary files?

Text files are human-readable, binary files are machine-readable.

Q4. What if file doesn’t exist in "r" mode?

fopen() returns NULL.

Q5. Why use fclose()?

To save data, release resources, and avoid corruption.

πŸ”‘ Key Points to Remember

  • FILE *fp is mandatory
  • Always check fp == NULL
  • Use correct file mode
  • Always close files using fclose()