B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP

C File Handling

File handling in C allows you to store and retrieve data from files. It is useful for saving program output, logs, and user data across program executions.

C में file handling का उपयोग डेटा को files में store और retrieve करने के लिए किया जाता है। यह user data या output को save रखने के लिए उपयोगी होता है।

Why Use Files in C?
In C programming, when you write output using printf() or read input using scanf(), that data only exists while the program is running. Once it ends, the data is lost.

Files allow data to be stored **permanently on disk**, so that the information can be reused later even after the program ends. This is very useful for storing:

  • User records
  • Program logs
  • Configuration settings
  • Large datasets (e.g., for databases)

C में File का उपयोग क्यों?
जब आप printf() या scanf() का उपयोग करते हैं, तो वो data केवल program के चलने तक memory में रहता है। Program बंद होते ही वह data खो जाता है।

Files का उपयोग करके हम data को permanently disk पर store कर सकते हैं ताकि उसे बाद में भी इस्तेमाल किया जा सके। इसका उपयोग अक्सर इन चीज़ों के लिए किया जाता है:

  • यूज़र का रिकॉर्ड सेव करने के लिए
  • Program के logs रखने के लिए
  • Configuration files में settings सेव करने के लिए
  • डेटाबेस जैसे बड़े डेटा संग्रह को स्टोर करने के लिए

Common File Handling Functions

  • fopen() - Opens a file in a specified mode (r, w, a, etc.).
  • fclose() - Closes the opened file.
  • fprintf() - Writes formatted output to a file.
  • fscanf() - Reads formatted input from a file.
  • fgets() - Reads a line or string from a file.
  • fputs() - Writes a string to a file.
  • fgetc() - Reads a single character from a file.
  • fputc() - Writes a single character to a file.
  • feof() - Checks if the end of a file is reached.
  • fseek() - Moves the file pointer to a specific position.
  • ftell() - Returns the current position of the file pointer.
  • rewind() - Moves the file pointer to the beginning.
  • fread() - Reads binary data from a file.
  • fwrite() - Writes binary data to a file.
  • remove() - Deletes a file.
  • rename() - Renames a file.
  • perror() - Prints an error message describing the last file error.
Example 1: Create a File
#include 

int main() {
  FILE *fp = fopen("example.txt", "w");
  if(fp) {
    printf("File created successfully");
    fclose(fp);
  } else {
    printf("File creation failed");
  }
  return 0;
}

Demonstrates: Create a File

fopen() का उपयोग करके नई फाइल बनाएं

Example 2: Write to File
#include 

int main() {
  FILE *fp = fopen("example.txt", "w");
  if(fp) {
    fprintf(fp, "Hello File Handling!\n");
    fclose(fp);
  }
  return 0;
}

Demonstrates: Write to File

fprintf() से फाइल में लिखना

Example 3: Read from File
#include 

int main() {
  char buffer[100];
  FILE *fp = fopen("example.txt", "r");
  if(fp) {
    while(fgets(buffer, 100, fp))
      printf("%s", buffer);
    fclose(fp);
  }
  return 0;
}

Demonstrates: Read from File

fgets() का उपयोग करके file पढ़ना

Example 4: Append to File
#include 

int main() {
  FILE *fp = fopen("example.txt", "a");
  if(fp) {
    fprintf(fp, "Appended Line\n");
    fclose(fp);
  }
  return 0;
}

Demonstrates: Append to File

फाइल में डेटा जोड़ना (append)

Example 5: Read Char by Char
#include 

int main() {
  FILE *fp = fopen("example.txt", "r");
  char c;
  if(fp) {
    while((c = fgetc(fp)) != EOF)
      putchar(c);
    fclose(fp);
  }
  return 0;
}

Demonstrates: Read Char by Char

fgetc() से character पढ़ना

Example 6: Write Char by Char
#include 

int main() {
  FILE *fp = fopen("char.txt", "w");
  if(fp) {
    fputc('A', fp);
    fclose(fp);
  }
  return 0;
}

Demonstrates: Write Char by Char

fputc() से character लिखना

Example 7: Check EOF
#include 

int main() {
  FILE *fp = fopen("example.txt", "r");
  char c;
  while(!feof(fp)) {
    c = fgetc(fp);
    putchar(c);
  }
  fclose(fp);
  return 0;
}

Demonstrates: Check EOF

feof() से फाइल खत्म होने की जांच

Example 8: Read & Write Both
#include 

int main() {
  FILE *fp = fopen("example.txt", "r+");
  if(fp) {
    fprintf(fp, "Modified Line\n");
    rewind(fp);
    char c;
    while((c = fgetc(fp)) != EOF)
      putchar(c);
    fclose(fp);
  }
  return 0;
}

Demonstrates: Read & Write Both

r+ मोड से पढ़ना और लिखना

Example 9: File Not Found
#include 

int main() {
  FILE *fp = fopen("nofile.txt", "r");
  if(fp == NULL)
    printf("File does not exist");
  else
    fclose(fp);
  return 0;
}

Demonstrates: File Not Found

File न मिलने पर NULL चेक करें

Example 10: Count Lines
#include 

int main() {
  FILE *fp = fopen("example.txt", "r");
  char ch; int count = 0;
  while((ch = fgetc(fp)) != EOF) {
    if(ch == '\n') count++;
  }
  printf("Total lines = %d", count);
  fclose(fp);
  return 0;
}

Demonstrates: Count Lines

फाइल में कितनी lines हैं गिनना

Example 11: File Size
#include 

int main() {
  FILE *fp = fopen("example.txt", "r");
  fseek(fp, 0, SEEK_END);
  long size = ftell(fp);
  printf("Size = %ld bytes", size);
  fclose(fp);
  return 0;
}

Demonstrates: File Size

ftell() से फाइल का size पता करें

Example 12: Binary Write
#include 

int main() {
  FILE *fp = fopen("data.bin", "wb");
  int num = 1234;
  fwrite(&num, sizeof(num), 1, fp);
  fclose(fp);
  return 0;
}

Demonstrates: Binary Write

fwrite() से binary लिखना

Example 13: Binary Read
#include 

int main() {
  FILE *fp = fopen("data.bin", "rb");
  int num;
  fread(&num, sizeof(num), 1, fp);
  printf("Value = %d", num);
  fclose(fp);
  return 0;
}

Demonstrates: Binary Read

fread() से binary पढ़ना

Example 14: Rename File
#include 

int main() {
  rename("example.txt", "renamed.txt");
  return 0;
}

Demonstrates: Rename File

rename() से फाइल का नाम बदलें

Example 15: Delete File
#include 

int main() {
  remove("renamed.txt");
  return 0;
}

Demonstrates: Delete File

remove() से फाइल delete करें