File से data read करना, write करना, अपडेट करना, file को create और delete करना आदि programming में file handling कहलाता है | data को parmanent store करने के लिए files का उपयोग किया जाता है क्योंकि program execution के दौरान data/information variable में स्टोर रहती, जो तभी तक रहता है जब तक program चल रहा है, program stop होने के बाद तो डाटा या infomation delete हो जाती है।
- जब किसी Software को उपयोग करते है तब कही न कही डाटा तो Generate होता ही है इस डाटा को स्टोर करने के लिए Database या Data Files (File) का उपयोग किया जाता है, Generally डाटा को स्टोर करने का सही स्थान Database है, लेकिन Data यदि Files में स्टोर करना तो हमें प्रोग्रामिंग की मदद से File Handling करके डाटा को स्टोर किया जाता है।
- आतंरिक रूप से Database भी data files में ही store करता है।
File को read करना
File को read करने के नीचे दिया गए syntax का अनुसरण करे –
r file को open करने का mode है
// open file in read mode
FILE *fptr = fopen("filename.txt","r");
Symbol | Mode | Description |
---|---|---|
r | Read mode | Read mode में केवल content को सिर्फ read कर सकते है। |
a | Append mode | Append mode में read करने के साथ-साथ content को write भी कर सकते है , file में जो पहले से content उसके last में new content जुड़जायेगा। |
w | Write mode | Write mode में content को file में save करते है , जो content पहले से होता वह delete हो जाता है, यदि file पहले से नहीं है तो new file create हो जाती है। |
Read File
यहाँ पर fptr का pointer है जो फाइल का address store करेगा, data एक character array variable है जो file के content को store करेगा, इसका size फाइल में content के according रख सकते है, file में डाटा ज्यादा है तो character variable का साइज बड़ा सकते है।
fopen एक built-in function है जो फाइल को open करता है, r mode है। fgets भी एक function है जो तीन argument लेता है-
१. variable जिसमे data/content स्टोर करना ही,
२. content size,
३. file pointer.
#include <stdio.h>
FILE *fptr;
int main() {
// store content of the file
char data[100];
// open file, named as test.text and mode is r(read)
fptr = fopen("test.txt", "r");
// store content of the file in data variable
fgets(data, 100, fptr);
// print
printf("%sn",data);
return 0;
}
Write to File
File में write करने के fprintf function का उपयोग किया जाता है, जिसमे 2 argument देने की आवश्यकता होती है
- File pointer
- content or variable
#include <stdio.h>
FILE *fptr;
int main() {
char data[100] = "this is information";
// open file, named as test.text and mode is w(write)
fptr = fopen("test.txt", "w");
fprintf(fptr,data);
return 0;
}
Append in File
File में new content जोड़ने के लिए उदाहरण का अनुसरण करे, new content, जो पहले से content है उसके बाद add होता है। content add करने से पहले file को append mode में open करना जरुरी है।
#include <stdio.h> FILE *fptr; int main() { char data[100] = "this is information"; // open file, named as test.text and mode is w(write) fptr = fopen("test.txt", "a"); fprintf(fptr,data); return 0; }
Close File
प्रोग्राम यदि file को open किया है तो उसे close करना जरुरी होता है, file को close करने के लिए fclose function का उपयोग किया जाता है। एक में function केवल एक argument देने की आवश्यता पड़ती यही जो की file pointer है।
#include <stdio.h> FILE *fptr; int main() { char data[100] = "this is information"; fptr = fopen("test.txt", "r"); puts(data); fclose(fptr); return 0; }
Conclusion
इस article में हम लोगों ने सीखा की C programing में किस तरह से file पर work किया जाता है और file handling क्या कहलाती है।