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 →
NULLreturn करता है
File Opening Modes
| Mode | Meaning |
|---|---|
"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:
FILE *fp;→ file pointer declare कियाfopen("file1.txt", "w");- file create/open करता है
- अगर file नहीं है → create होगी
- अगर है → overwrite होगी
fp == NULLcheck- अगर file open नहीं हुई → error
- success message print
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 नहीं करती →
NULLreturn होगा - इसलिए 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
- file open की गई
"w"mode में fprintf()से data file में लिखा गयाfclose()से file close की गई- 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() होता है।