fopen and fclose in C

Introduction

C language में file handling की शुरुआत file को open और close करने से होती है।

इसके लिए दो important functions use होते हैं:

  • fopen() → file को open करने के लिए
  • fclose() → file को close करने के लिए

बिना fopen() के file access नहीं कर सकते
बिना fclose() के file properly save नहीं होती

1. fopen() Function

Definition

fopen() का उपयोग file को open करने के लिए किया जाता है।

Syntax

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

Parameters

  • "filename" → file का नाम
  • "mode" → file किस mode में open होगी

Return Value

  • Success → file pointer return करता है
  • Failure → NULL return करता है

File Opening Modes

ModeMeaning
"r"Read (file exist होनी चाहिए)
"w"Write (नई file create या overwrite)
"a"Append (end में data add)
"r+"Read + Write
"w+"Read + Write (overwrite)
"a+"Read + Append

Example Program 1: File Create करना (w mode)

#include <stdio.h>

int main() {
FILE *fp;

fp = fopen("file1.txt", "w");

if (fp == NULL) {
printf("File not created\n");
return 1;
}
printf("File created successfully\n");

fclose(fp);

return 0;
}

Program Explanation

Step-by-step:

  1. FILE *fp; → file pointer declare किया
  2. fopen("file1.txt", "w");
    • file create/open करता है
    • अगर file नहीं है → create होगी
    • अगर है → overwrite होगी
  3. fp == NULL check
    • अगर file open नहीं हुई → error
  4. success message print
  5. fclose(fp);
    • file close कर दी गई

Output:

File created successfully

Example Program 2: File Read करना (r mode)

#include <stdio.h>

int main() {

FILE *fp;

fp = fopen("file1.txt", "r");

if (fp == NULL) {
printf("File not found\n");
return 1;
}

printf("File opened in read mode\n");

fclose(fp);

return 0;
}

Program Explanation

  • "r" mode में file open होती है
  • अगर file exist नहीं करती → NULL return होगा
  • इसलिए error handling जरूरी है

Output (if file exists):

File opened in read mode

2. fclose() Function

Definition

fclose() का उपयोग file को close करने के लिए किया जाता है।

Syntax

fclose(file_pointer);

Example:

fclose(fp);

fclose() का Importance

1. Data Save करता है

File में लिखा गया data properly save होता है

2. Memory Free करता है

System resources release होते हैं

3. Data Corruption रोकता है

File safe रहती है

Example Program 3: Write + Close

#include <stdio.h>

int main() {

FILE *fp;

fp = fopen("file2.txt", "w");

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

fprintf(fp, "Hello File Handling\n");

fclose(fp);

printf("Data written and file closed\n");

return 0;
}

Program Explanation

  1. file open की गई "w" mode में
  2. fprintf() से data file में लिखा गया
  3. fclose() से file close की गई
  4. data permanent storage में save हो गया

Output:

Data written and file closed

Common Mistakes

fclose() भूल जाना
fp == NULL check नहीं करना
गलत mode use करना

Important Points

  • fopen() हमेशा file pointer return करता है
  • fclose() file को close करता है
  • हर file open के बाद close करना जरूरी है
  • error handling बहुत important है

Exam / Practical Tips

  • fopen() + fclose() basic question हमेशा आता है
  • modes (r, w, a) याद रखो
  • NULL checking जरूर लिखो

Conclusion

fopen() और fclose() file handling के सबसे basic और important functions हैं।

इनकी मदद से हम file को open, use और safely close कर सकते हैं।
हर file operation का पहला step fopen() और last step fclose() होता है।

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top