जब हम C programming में कोई program run करते हैं, तो variables में store किया गया data temporary होता है। Program बंद होते ही यह data RAM से delete हो जाता है। लेकिन real life applications में हमें data को permanently save करने की जरूरत होती है, जैसे – student records, marks, bank details, logs आदि।
यहीं पर File Handling in C का concept आता है। File handling हमें data को file में store, read, update और delete करने की सुविधा देता है। इस article में हम File Handling in C को beginner से लेकर pro level तक step-by-step समझेंगे।
C में फ़ाइल हैंडलिंग क्या है (What is File Handling in C Programming) ?
File Handling in C एक technique है, जिसकी मदद से हम C program के द्वारा files में data को store और access कर सकते हैं।
File handling के बिना program केवल runtime तक ही useful रहता है, जबकि file handling से program real-world application बन जाता है।
यह concept exam, interview और practical projects तीनों के लिए बहुत important है।
Types of Files in C Programming
C language में mainly दो प्रकार की files होती हैं- Text Files and Binary Files:
Text Files
C programming में Text File वह file होती है जिसमें data human-readable form में store किया जाता है, यानी जिसे हम आसानी से पढ़ और समझ सकते हैं। Text file में data characters के रूप में ASCII format में save होता है ।
जैसे letters, numbers और symbols। .txt और .csv text files के common examples हैं। C में text file को handle करने के लिए fgetc(), fgets(), fprintf() जैसे functions का use किया जाता है।
Use case:
- Reports
- Logs
- Simple data storage
Binary Files
Binary File वह file होती है जिसमें data binary (0 और 1) form में store किया जाता है, जिसे इंसान सीधे पढ़ नहीं सकता। Binary files ज्यादा secure और fast होती हैं, इसलिए large data और structure-based data को store करने के लिए इन्हें prefer किया जाता है।
C programming में binary files को read और write करने के लिए fread() और fwrite() functions का use किया जाता है।
Use case:
- Large data
- Structure data
- Database-like storage
File Handling in C में binary files ज्यादा powerful मानी जाती हैं।
यह भी पढ़ें: सी प्रोग्रामिंग में इनपुट फंक्शन क्या हैं | What is input function in C programming in Hindi
File Handling in C – Basic Concepts
File handling को समझने से पहले कुछ basic concepts को clear करना बहुत ज़रूरी है, खासकर beginners के लिए। जब हम C program run करते हैं, तब data memory (RAM) में store होता है और program बंद होते ही यह data खत्म हो जाता है। लेकिन files की मदद से हम data को computer की hard disk में permanently save कर सकते हैं।
C programming में file के साथ काम करने के लिए हमें एक special pointer का use करना पड़ता है, जिसे FILE pointer कहा जाता है। यह pointer file का address store करता है और program को बताता है कि file कहाँ मौजूद है। इसे आमतौर पर इस तरह declare किया जाता है:
FILE *fp;
जब भी हम किसी file को open करते हैं, तो यह pointer उस file से connect हो जाता है। अगर किसी कारण से file open नहीं होती है, तो FILE pointer NULL return करता है। इसलिए beginners के लिए यह बहुत जरूरी है कि वे हमेशा file open करने के बाद NULL check करें।
इन basic concepts को अच्छे से समझ लेने के बाद File Handling in C सीखना आसान और interesting हो जाता है, क्योंकि यही foundation आगे के सभी file operations का आधार होती है।
File Modes in C
File Modes in C programming यह बताते हैं कि किसी file को किस purpose से open किया जा रहा है, यानी हम file को read, write या append करना चाहते हैं। जब भी C में किसी file को fopen() function की मदद से open किया जाता है, तो उसके साथ एक file mode specify करना जरूरी होता है। सही file mode का चयन करना बहुत important है, क्योंकि गलत mode use करने से data loss या error हो सकता है।
File modes यह decide करते हैं कि file पहले से मौजूद होनी चाहिए या नहीं, पुराना data delete होगा या नहीं, और file में नया data कहाँ add होगा।
| Mode | Description |
| r | Read only |
| w | Write (old data delete) |
| a | Append |
| r+ | Read + Write |
| w+ | Write + Read |
| a+ | Append + Read |
| rb | Read binary |
| wb | Write binary |
इस प्रकार, File Modes in C programming file handling का एक बहुत जरूरी हिस्सा हैं, क्योंकि इनके बिना हम files के साथ सही और सुरक्षित तरीके से काम नहीं कर सकते।
यह भी पढ़ें: सी प्रोग्रामिंग में ऑपरेटर क्या है | What is Operator in C Programming in Hindi
Important File Handling Functions in C
C programming में File Handling करने के लिए कुछ important built-in functions दिए गए हैं। इन functions की मदद से हम file को open, close, read और write कर सकते हैं। Beginners के लिए इन functions को step-by-step समझना बहुत जरूरी है, क्योंकि यही functions practically हर C program में use होते हैं।
1. fopen() Function
fopen() function का use file को open करने के लिए किया जाता है। यह function file का नाम और file mode लेता है और एक FILE pointer return करता है।
Syntax:
FILE *fp;
fp = fopen("file.txt", "r");
अगर file successfully open हो जाती है, तो fp file को point करता है।
अगर file open नहीं होती, तो यह function NULL return करता है।
Example:
FILE *fp;
fp = fopen("data.txt", "r");
if(fp == NULL) {
printf("File open nahi ho rahi hai");
}
else {
printf("File successfully open ho gayi");
}
2. fclose() Function
fclose() function का use open file को close करने के लिए किया जाता है। File close करना बहुत जरूरी होता है, ताकि data properly save हो सके।
Syntax:
fclose(fp);
Example:
FILE *fp;
fp = fopen("data.txt", "w");
fprintf(fp, "Hello C Programming");
fclose(fp);
3. fgetc() Function
fgetc() function का use file से single character read करने के लिए किया जाता है।
Syntax:
ch = fgetc(fp);
Example:
FILE *fp;
char ch;
fp = fopen("data.txt", "r");
while((ch = fgetc(fp)) != EOF) {
printf("%c", ch);
}
fclose(fp);
4. fputc() Function
fputc() function का use file में single character write करने के लिए किया जाता है।
Syntax:
fputc(character, fp);
Example:
FILE *fp;
fp = fopen("data.txt", "w");
fputc('A', fp);
fputc('B', fp);
fclose(fp);
5. fgets() Function
fgets() function का use file से एक पूरी line read करने के लिए किया जाता है।
Syntax:
fgets(string, size, fp);
Example:
FILE *fp;
char str[50];
fp = fopen("data.txt", "r");
fgets(str, 50, fp);
printf("%s", str);
fclose(fp);
6. fputs() Function
fputs() function का use file में string या line write करने के लिए किया जाता है।
Syntax:
fputs(string, fp);
Example:
FILE *fp;
fp = fopen("data.txt", "w");
fputs("Welcome to C File Handling", fp);
fclose(fp);
7. fprintf() Function
fprintf() function formatted data को file में write करने के लिए use होता है।
Syntax:
fprintf(fp, "format", variables);
Example:
FILE *fp;
int roll = 10;
char name[] = "Ravi";
fp = fopen("student.txt", "w");
fprintf(fp, "Roll: %d Name: %s", roll, name);
fclose(fp);
8. fscanf() Function
fscanf() function file से formatted data read करने के लिए use होता है।
Syntax:
fscanf(fp, "format", &variables);
Example:
FILE *fp;
int roll;
char name[20];
fp = fopen("student.txt", "r");
fscanf(fp, "%d %s", &roll, name);
printf("Roll: %d Name: %s", roll, name);
fclose(fp);
9. fread() Function
fread() function binary file से data read करने के लिए use होता है।
Syntax:
fread(&var, size, count, fp);
Example:
FILE *fp;
int num;
fp = fopen("num.bin", "rb");
fread(&num, sizeof(num), 1, fp);
printf("Number: %d", num);
fclose(fp);
10. fwrite() Function
fwrite() function binary file में data write करने के लिए use होता है।
Syntax:
fwrite(&var, size, count, fp);
Example:
FILE *fp;
int num = 100;
fp = fopen("num.bin", "wb");
fwrite(&num, sizeof(num), 1, fp);
fclose(fp);
यह भी पढ़ें: सी प्रोग्रामिंग के बेसिक स्ट्रक्चर क्या है | Basic Structure of C Programming in Hindi
File Handling in C Using fprintf() and fscanf()
यह functions formatted file I/O के लिए use होती हैं।
fprintf(fp, "%d %s", roll, name); fscanf(fp, "%d %s", &roll, name);
Difference समझिए:
- printf() → screen
- fprintf() → file
यह File Handling in C का बहुत practical part है।
File Handling in C Using fread() and fwrite()
Binary files में data read/write करने के लिए।
fwrite(&s, sizeof(s), 1, fp); fread(&s, sizeof(s), 1, fp);
यह fast होता है और large data के लिए best है।
Reading and Writing Structure to File in C
Real applications में data structure form में store होता है।
struct student {
int roll;
char name[20];
};
Structure को binary file में store करना efficient होता है।
यह concept projects और interviews में बहुत पूछा जाता है।
Advantages of File Handling in C
- File handling की मदद से data को hard disk में permanently store किया जा सकता है, जिससे program बंद होने के बाद भी data सुरक्षित रहता है।
- C programming में file handling का use करके बहुत बड़े data को efficiently store और manage किया जा सकता है, जो memory (RAM) में possible नहीं होता।
- Files में store किया गया data बार-बार use किया जा सकता है, जिससे program को दोबारा run करने पर same data फिर से access किया जा सकता है।
- Banking software, school management system, billing system और database-like applications में file handling का व्यापक रूप से use होता है।
- Files की help से data का backup बनाना आसान होता है, जिससे system failure या crash की स्थिति में data loss से बचा जा सकता है।
- C में structures को files में store किया जा सकता है, जिससे data organized और systematic तरीके से maintain होता है।
- File handling program को ज्यादा powerful और practical बनाती है, जिससे simple program real-world software में convert हो जाता है।
Disadvantages of File Handling in C
- File handling operations RAM की तुलना में slow होते हैं, क्योंकि data hard disk से read और write किया जाता है।
- Beginners के लिए file handling समझना थोड़ा difficult होता है, क्योंकि इसमें file pointer, modes और error handling जैसे concepts शामिल होते हैं।
- अगर file को properly close न किया जाए या wrong file mode use किया जाए, तो data corrupt होने का risk रहता है।
- File open न होने, permission issues या path error जैसी समस्याओं को handle करना जरूरी होता है, वरना program crash कर सकता है।
- Binary files एक system पर create होकर दूसरे system पर compatibility issue create कर सकती हैं।
- Simple file handling में data encryption नहीं होता, जिससे sensitive data secure नहीं रहता जब तक additional security techniques use न की जाएं।
- Multiple files को manage करना difficult हो सकता है, खासकर large applications में, जिससे program complexity बढ़ जाती है।
Real-Life Applications of File Handling in C
- Banking software
- School management system
- Hospital records
- Billing & invoice software
लगभग हर professional software में File Handling in C use होती है।
Video Explanation of File Handling in C
अगर आपको File Handling in C Programming को practically और visually समझना है, तो निचे दिया गया video आपके लिए बहुत helpful रहेगा। इस video में file handling के concepts को step-by-step explain किया गया है, जहाँ आप real C programs के साथ देख सकते हैं कि file कैसे open होती है, data कैसे write और read किया जाता है, और common mistakes से कैसे बचा जाए।
Beginners के लिए यह video learning को और भी आसान बना देता है, क्योंकि theory के साथ practical demonstration देखने से concepts जल्दी clear होते हैं। इसलिए article पढ़ने के बाद इस video को जरूर देखें और साथ-साथ code practice करें।
निष्कर्ष (Conclusion)
इस लेख में आपने File Handling in C Programming को बिल्कुल आसान और सरल भाषा में समझा। हमने जाना कि file handling क्या होती है, इसकी जरूरत क्यों पड़ती है, text और binary files में क्या अंतर है, साथ ही C में इस्तेमाल होने वाले important functions, file modes, advantages और disadvantages के बारे में भी detail में चर्चा की।
आज के समय में केवल program लिखना ही काफी नहीं है, बल्कि data को permanently store और manage करना भी उतना ही जरूरी है। यही वजह है कि File Handling in C real-world software development, exams और interviews तीनों के लिए बहुत महत्वपूर्ण topic है। अगर आप C programming में strong foundation बनाना चाहते हैं, तो file handling की अच्छी practice जरूर करें।
अंत में यही कहा जा सकता है कि file handling C language को ज्यादा powerful और practical बनाती है। जितना ज्यादा आप examples पर काम करेंगे, उतना ही यह concept आपको आसान और interesting लगेगा। उम्मीद है यह लेख आपकी learning journey में मददगार साबित होगा।
FAQs
Q1. C language में file handling की जरूरत क्यों होती है?
Ans. C में variables temporary होते हैं और program बंद होते ही data delete हो जाता है। File handling की मदद से data hard disk में permanently store किया जाता है, जिससे future में उसे फिर से use किया जा सके।
Q2. Text file और Binary file में क्या अंतर है?
Ans. Text file human-readable होती है और data ASCII format में store होता है, जबकि binary file machine-readable होती है और data binary form में store होता है। Binary files fast और secure होती हैं।
Q3. File Handling in C exams और interviews के लिए क्यों important है?
Ans. File Handling in C से जुड़े सवाल exams, viva और interviews में अक्सर पूछे जाते हैं क्योंकि यह real-life programming का important part है। Banking, school management और billing software में file handling का व्यापक use होता है।
You May Also Read Best Resources of File Handling in C:
- From GeeksForGeeks
- From Byjus