C फ़ाइलें I / O: किसी फ़ाइल को खोलना, पढ़ना, लिखना और बंद करना

इस ट्यूटोरियल में, आप सी में फाइल हैंडलिंग के बारे में जानेंगे। आप fprintf (), fscanf (), freadite (), fwrite (), fseek () आदि की मदद से मानक I / O को C में हैंडल करना सीखेंगे। उदाहरण।

एक फ़ाइल कंप्यूटर भंडारण उपकरणों में एक कंटेनर है जो डेटा संग्रहीत करने के लिए उपयोग किया जाता है।

फ़ाइलों की आवश्यकता क्यों है?

  • जब कोई प्रोग्राम समाप्त होता है, तो पूरा डेटा खो जाता है। एक फ़ाइल में भंडारण कार्यक्रम समाप्त होने पर भी आपके डेटा को संरक्षित करेगा।
  • यदि आपको बड़ी संख्या में डेटा दर्ज करना है, तो उन सभी को दर्ज करने में बहुत समय लगेगा।
    हालाँकि, यदि आपके पास सभी डेटा वाली फ़ाइल है, तो आप C में कुछ कमांड का उपयोग करके आसानी से फ़ाइल की सामग्री तक पहुँच सकते हैं।
  • आप अपने डेटा को बिना किसी बदलाव के आसानी से एक कंप्यूटर से दूसरे कंप्यूटर पर ले जा सकते हैं।

फ़ाइलों के प्रकार

फ़ाइलों के साथ काम करते समय, दो प्रकार की फाइलें होती हैं जिनके बारे में आपको पता होना चाहिए:

  1. पाठ फ़ाइलें
  2. बाइनरी फाइलें

1. पाठ फ़ाइलें

टेक्स्ट फाइलें सामान्य .txt फाइलें हैं। आप किसी भी साधारण टेक्स्ट एडिटर जैसे नोटपैड का उपयोग करके आसानी से टेक्स्ट फाइल बना सकते हैं।

जब आप उन फ़ाइलों को खोलते हैं, तो आपको फ़ाइल के भीतर की सभी सामग्री सादे पाठ के रूप में दिखाई देगी। आप सामग्री को आसानी से संपादित या हटा सकते हैं।

वे बनाए रखने के लिए न्यूनतम प्रयास करते हैं, आसानी से पठनीय हैं, और कम से कम सुरक्षा प्रदान करते हैं और बड़े भंडारण स्थान लेते हैं।

2. बाइनरी फाइलें

बाइनरी फाइलें ज्यादातर आपके कंप्यूटर में .bin फाइलें होती हैं।

सादे पाठ में डेटा संग्रहीत करने के बजाय, वे इसे बाइनरी फॉर्म (0 और 1) में संग्रहीत करते हैं।

वे अधिक मात्रा में डेटा पकड़ सकते हैं, आसानी से पढ़ने योग्य नहीं हैं, और पाठ फ़ाइलों की तुलना में बेहतर सुरक्षा प्रदान करते हैं।

फ़ाइल संचालन

C में, आप पाठ या बाइनरी पर चार बड़े ऑपरेशन कर सकते हैं:

  1. एक नई फ़ाइल बनाना
  2. किसी मौजूदा फ़ाइल को खोलना
  3. एक फ़ाइल बंद करना
  4. फ़ाइल से जानकारी पढ़ना और लिखना

फाइलों के साथ काम करना

फ़ाइलों के साथ काम करते समय, आपको टाइप फ़ाइल का पॉइंटर घोषित करने की आवश्यकता होती है। यह घोषणा फ़ाइल और कार्यक्रम के बीच संचार के लिए आवश्यक है।

 FILE *fptr;

एक फ़ाइल खोलना - निर्माण और संपादन के लिए

हेडर फ़ाइल fopen()में परिभाषित फ़ंक्शन का उपयोग करके एक फ़ाइल खोलना किया जाता है stdio.h

मानक I / O में फ़ाइल खोलने का सिंटैक्स है:

 ptr = fopen("fileopen","mode"); 

उदाहरण के लिए,

 fopen("E:\cprogram\newprogram.txt","w"); fopen("E:\cprogram\oldprogram.bin","rb");
  • मान लें कि फ़ाइल newprogram.txtस्थान में मौजूद नहीं है E:cprogram। पहला फ़ंक्शन नाम की एक नई फ़ाइल बनाता है newprogram.txtऔर इसे 'w' मोड के अनुसार लिखने के लिए खोलता है ।
    लेखन मोड आपको फ़ाइल की सामग्री बनाने और संपादित (ओवरराइट) करने की अनुमति देता है।
  • अब मान लें कि स्थान में दूसरी बाइनरी फ़ाइल oldprogram.binमौजूद है E:cprogram। दूसरा फ़ंक्शन बाइनरी मोड 'आरबी' में पढ़ने के लिए मौजूदा फ़ाइल खोलता है ।
    रीडिंग मोड आपको केवल फ़ाइल पढ़ने की अनुमति देता है, आप फ़ाइल में नहीं लिख सकते।
मानक I / O में उद्घाटन मोड
मोड मीनिंग ऑफ मोड फ़ाइल की अनुपस्थिति के दौरान
r पढ़ने के लिए खोलें। यदि फ़ाइल मौजूद नहीं है, तो fopen()NULL लौटाता है।
rb बाइनरी मोड में पढ़ने के लिए खोलें। यदि फ़ाइल मौजूद नहीं है, तो fopen()NULL लौटाता है।
w लिखने के लिए खोलें। यदि फ़ाइल मौजूद है, तो इसकी सामग्री ओवरराइट की गई है।
यदि फ़ाइल मौजूद नहीं है, तो इसे बनाया जाएगा।
wb बाइनरी मोड में लिखने के लिए खोलें। यदि फ़ाइल मौजूद है, तो इसकी सामग्री ओवरराइट की गई है।
यदि फ़ाइल मौजूद नहीं है, तो इसे बनाया जाएगा।
a एपेंड के लिए खोलें।
डेटा फ़ाइल के अंत में जोड़ा जाता है।
यदि फ़ाइल मौजूद नहीं है, तो इसे बनाया जाएगा।
ab बाइनरी मोड में एपेंड के लिए खोलें।
डेटा फ़ाइल के अंत में जोड़ा जाता है।
यदि फ़ाइल मौजूद नहीं है, तो इसे बनाया जाएगा।
r+ पढ़ने और लिखने दोनों के लिए खुला। यदि फ़ाइल मौजूद नहीं है, तो fopen()NULL लौटाता है।
rb+ बाइनरी मोड में पढ़ने और लिखने दोनों के लिए खोलें। यदि फ़ाइल मौजूद नहीं है, तो fopen()NULL लौटाता है।
w+ पढ़ने और लिखने दोनों के लिए खुला। यदि फ़ाइल मौजूद है, तो इसकी सामग्री ओवरराइट की गई है।
यदि फ़ाइल मौजूद नहीं है, तो इसे बनाया जाएगा।
wb+ बाइनरी मोड में पढ़ने और लिखने दोनों के लिए खोलें। यदि फ़ाइल मौजूद है, तो इसकी सामग्री ओवरराइट की गई है।
यदि फ़ाइल मौजूद नहीं है, तो इसे बनाया जाएगा।
a+ पढ़ने और जोड़ने दोनों के लिए खुला। यदि फ़ाइल मौजूद नहीं है, तो इसे बनाया जाएगा।
ab+ बाइनरी मोड में पढ़ना और जोड़ना दोनों के लिए खुला है। यदि फ़ाइल मौजूद नहीं है, तो इसे बनाया जाएगा।

फ़ाइल बंद करना

फ़ाइल (पाठ और बाइनरी दोनों) पढ़ने / लिखने के बाद बंद होनी चाहिए।

fclose()फ़ंक्शन का उपयोग करके किसी फ़ाइल को बंद करना किया जाता है ।

 fclose(fptr);

Here, fptr is a file pointer associated with the file to be closed.

Reading and writing to a text file

For reading and writing to a text file, we use the functions fprintf() and fscanf().

They are just the file versions of printf() and scanf(). The only difference is that fprint() and fscanf() expects a pointer to the structure FILE.

Example 1: Write to a text file

 #include #include int main() ( int num; FILE *fptr; // use appropriate location if you are using MacOS or Linux fptr = fopen("C:\program.txt","w"); if(fptr == NULL) ( printf("Error!"); exit(1); ) printf("Enter num: "); scanf("%d",&num); fprintf(fptr,"%d",num); fclose(fptr); return 0; ) 

This program takes a number from the user and stores in the file program.txt.

After you compile and run this program, you can see a text file program.txt created in C drive of your computer. When you open the file, you can see the integer you entered.

Example 2: Read from a text file

 #include #include int main() ( int num; FILE *fptr; if ((fptr = fopen("C:\program.txt","r")) == NULL)( printf("Error! opening file"); // Program exits if the file pointer returns NULL. exit(1); ) fscanf(fptr,"%d", &num); printf("Value of n=%d", num); fclose(fptr); return 0; ) 

This program reads the integer present in the program.txt file and prints it onto the screen.

If you successfully created the file from Example 1, running this program will get you the integer you entered.

Other functions like fgetchar(), fputc() etc. can be used in a similar way.

Reading and writing to a binary file

Functions fread() and fwrite() are used for reading from and writing to a file on the disk respectively in case of binary files.

Writing to a binary file

To write into a binary file, you need to use the fwrite() function. The functions take four arguments:

  1. address of data to be written in the disk
  2. size of data to be written in the disk
  3. number of such type of data
  4. pointer to the file where you want to write.
 fwrite(addressData, sizeData, numbersData, pointerToFile);

Example 3: Write to a binary file using fwrite()

 #include #include struct threeNum ( int n1, n2, n3; ); int main() ( int n; struct threeNum num; FILE *fptr; if ((fptr = fopen("C:\program.bin","wb")) == NULL)( printf("Error! opening file"); // Program exits if the file pointer returns NULL. exit(1); ) for(n = 1; n < 5; ++n) ( num.n1 = n; num.n2 = 5*n; num.n3 = 5*n + 1; fwrite(&num, sizeof(struct threeNum), 1, fptr); ) fclose(fptr); return 0; ) 

In this program, we create a new file program.bin in the C drive.

We declare a structure threeNum with three numbers - n1, n2 and n3, and define it in the main function as num.

Now, inside the for loop, we store the value into the file using fwrite().

The first parameter takes the address of num and the second parameter takes the size of the structure threeNum.

Since we're only inserting one instance of num, the third parameter is 1. And, the last parameter *fptr points to the file we're storing the data.

Finally, we close the file.

Reading from a binary file

Function fread() also take 4 arguments similar to the fwrite() function as above.

 fread(addressData, sizeData, numbersData, pointerToFile);

Example 4: Read from a binary file using fread()

 #include #include struct threeNum ( int n1, n2, n3; ); int main() ( int n; struct threeNum num; FILE *fptr; if ((fptr = fopen("C:\program.bin","rb")) == NULL)( printf("Error! opening file"); // Program exits if the file pointer returns NULL. exit(1); ) for(n = 1; n < 5; ++n) ( fread(&num, sizeof(struct threeNum), 1, fptr); printf("n1: %d n2: %d n3: %d", num.n1, num.n2, num.n3); ) fclose(fptr); return 0; ) 

In this program, you read the same file program.bin and loop through the records one by one.

In simple terms, you read one threeNum record of threeNum size from the file pointed by *fptr into the structure num.

You'll get the same records you inserted in Example 3.

Getting data using fseek()

If you have many records inside a file and need to access a record at a specific position, you need to loop through all the records before it to get the record.

This will waste a lot of memory and operation time. An easier way to get to the required data can be achieved using fseek().

As the name suggests, fseek() seeks the cursor to the given record in the file.

Syntax of fseek()

 fseek(FILE * stream, long int offset, int whence);

The first parameter stream is the pointer to the file. The second parameter is the position of the record to be found, and the third parameter specifies the location where the offset starts.

फ़ेसेक () में विभिन्न स्थान
किसका अर्थ
SEEK_SET फ़ाइल की शुरुआत से ऑफसेट शुरू करता है।
SEEK_END फ़ाइल के अंत से ऑफसेट शुरू करता है।
SEEK_CUR फ़ाइल में कर्सर के वर्तमान स्थान से ऑफसेट शुरू करता है।

उदाहरण 5: छद्म ()

 #include #include struct threeNum ( int n1, n2, n3; ); int main() ( int n; struct threeNum num; FILE *fptr; if ((fptr = fopen("C:\program.bin","rb")) == NULL)( printf("Error! opening file"); // Program exits if the file pointer returns NULL. exit(1); ) // Moves the cursor to the end of the file fseek(fptr, -sizeof(struct threeNum), SEEK_END); for(n = 1; n < 5; ++n) ( fread(&num, sizeof(struct threeNum), 1, fptr); printf("n1: %d n2: %d n3: %d", num.n1, num.n2, num.n3); fseek(fptr, -2*sizeof(struct threeNum), SEEK_CUR); ) fclose(fptr); return 0; ) 

यह प्रोग्राम program.binरिवर्स ऑर्डर में फ़ाइल से रिकॉर्ड (आखिरी से पहली) पढ़ना शुरू करेगा और उसे प्रिंट करेगा।

दिलचस्प लेख...