फाइल रिकॉर्ड्स का संग्रह है (या हार्ड डिस्क पर वह स्थान जहां डेटा स्थायी रूप से संग्रहीत होता है)।
C कमांड्स का उपयोग करके, हम फाइलों को विभिन्न तरीकों से एक्सेस कर सकते हैं।
फाइलों पर ऑपरेशन्स
C प्रोग्रामिंग भाषा में फाइलों पर किए जा सकने वाले ऑपरेशन्स निम्नलिखित हैं:
- फाइल का नामकरण
- फाइल खोलना
- फाइल से पढ़ना
- फाइल में लिखना
- फाइल बंद करना
सिंटैक्स
फाइल खोलने और नामकरण का सिंटैक्स क्रमशः नीचे दिया गया है:
FILE *फाइल_पॉइंटर;
उदाहरण के लिए, FILE *fptr;
फाइल_पॉइंटर = fopen("फाइल_नाम", "मोड");
उदाहरण के लिए, fptr = fopen("sample.txt", "r");
FILE *fp;
fp = fopen("sample.txt", "w");
फाइल से पढ़ने का सिंटैक्स:
int fgetc(FILE *fp); // फाइल से एक कैरेक्टर पढ़ें
फाइल में लिखने का सिंटैक्स:
int fputc(int c, FILE *fp); // स्ट्रीम में व्यक्तिगत कैरेक्टर लिखें
वर्तमान डायरेक्टरी में फाइलों और फोल्डर्स को प्रदर्शित करने के लिए उपयोग की जाने वाली लॉजिक, जहां प्रोग्राम सेव है:
dr = opendir(".");
if(dr != NULL) {
printf("फाइलों और फोल्डर्स की सूची:-\n");
for(d = readdir(dr); d != NULL; d = readdir(dr)) {
printf("%s\n", d->d_name);
}
closedir(dr);
}
उदाहरण
डायरेक्टरी में फाइलों और फोल्डर्स प्रिंट करने का C प्रोग्राम:
#include <stdio.h>
#include <conio.h>
#include <dirent.h>
int main() {
struct dirent *d;
DIR *dr;
dr = opendir(".");
if(dr != NULL) {
printf("फाइलों और फोल्डर्स की सूची:-\n");
for(d = readdir(dr); d != NULL; d = readdir(dr)) {
printf("%s\n", d->d_name);
}
closedir(dr);
} else
printf("\nडायरेक्टरी खोलने में त्रुटि!");
getch();
return 0;
}
आउटपुट
फाइलों और फोल्डर्स की सूची:- . .. accessing array.c accessing array.exe accessing array.o bhanu.txt C Programs convert 2 digit no into english word.c convert 2 digit no into english word.exe convert 2 digit no into english word.o DATA delete vowels in string.c delete vowels in string.exe delete vowels in string.o emp.txt EVEN ex.c ex.exe ex.o example pro.c example pro.exe example pro.o fibbinoci serie.c fibbinoci serie.exe fibbinoci serie.o file file example1.c file example1.exe file example1.o file example2.c file example2.exe file example2.o implicit conversion.c implicit conversion.exe implicit conversion.o leap year.c leap year.exe leap year.o little n big endian.c little n big endian.exe little n big endian.o work out examples