C में File handling और उसके function की संपूर्ण जानकारी

File handling in C in Hindi

C में File handling के अंतर्गत File बनाने, खोलने, पढ़ने और उसे बंद करने से संबंधित कार्य किए जाते है।

यह Programs को external files के साथ Interact करने जैसे Data input, Output और उसमे हेरफेर(Manipulate) करने की अनुमति देते है।

C language File संबंधित कार्यों को करने के लिए विभिन्न प्रकार के Functions प्रदान करता है 

File operations in C in Hindi

एक File में निम्न Operations Perform किया जा सकता है
1) New file का निर्माण करना
2) पहले से उपस्थित File को Open करना
3) File से पढ़ना
4) File में लिखना
5) File को Delete करना

C में File handling की जरुरत क्यों  है?

C में File handling आवश्यक है क्योंकि यह programs को Data read करने और External file में Data write करने की अनुमति देता है हमें निम्न कारणों से C में File handle करने की जरूरत होती है 

1) Program के समाप्त हो जाने पर Files हमें Data को स्थाई रूप से store करने का एक तरीका प्रदान करता है यह Program run के बीच Data को संरक्षित करने की अनुमति देता है।

2) Files विभिन्न program और systems के बीच data sharing को सक्रिय करता है 

3) Files,  Data backup के साधन के रूप में कार्य करती हैं महत्वपूर्ण जानकारी को Files में सुरक्षित रखा जा सकता है, जिससे Program या System के fail हो जानें कि स्थिति में Data को loss होने से बचाया जा सकता है

4) Files बड़ी मात्रा में Data को कुशलतापूर्वक Process करने की अनुमति देती हैं Program Files से Data को टुकड़ों में पढ़ सकते हैं या File structure के आधार पर Data को लाइन दर लाइन Process कर सकते हैं।

5) Files विभिन्न सॉफ़्टवेयर सिस्टम और प्लेटफ़ॉर्म के बीच Data exchange की सुविधा प्रदान करती हैं उदाहरण के लिए,Data को C प्रोग्राम से एक File में ऐसे format में Export किया जा सकता है जिसे अन्य Software application में Import किया जा सकता है।

Types of File in C in Hindi

C programming में निम्न प्रकार की files होती है जिसके साथ हम कार्य कर सकते हैं
1) Text Files
2) Binary Files
3) Special Files

Text Files

ये Files Data को एक human-readable format में Store करता है
Text files में plain text, numbers, और अन्य characters शामिल होते हैं जिसे किसी भी text editor में पढ़ा और लिखा जा सकता है

Text files में सामान्यत: Source Code को भी Store किया जाता है

Binary Files

Binary files में store किए गए data human-readable format में नही होता इसमें Data, binary form अर्थात (0,1) के Store होता है,

इस Files में विभिन्न प्रकार की सूचनाएं जैसे images, audio, video, executables, और structured data Store की जा सकती है

Binary files को एक Program के अंदर ही बनाया जाता है और उसकी सूचनाओं को एक Program के द्वारा ही पढ़ा जा सकता है

Special Files

C में Special files का Use input और output operations के लिए होता है जैसे stdin (standard input), stdout (standard output) और stderr (standard error) आदि C की library Header file है 

जिसका Use अक्सर Console के साथ Interact करने और Programs में input/output streams को handle करने के लिए होता है

File pointer in C in Hindi - C में File pointer

C programming में एक File pointer एक विशेष प्रकार का pointer होता है जिसका प्रयोग files को handle करने के लिए किया जाता है

यह खुली हुई file के एक विशिष्ट position को refer करती है
file pointer एक structure है जो कुछ सूचनाओं जैसे file का नाम, उसका स्थान और उसके mode जिसमे file access होते है आदि को रखते है

File pointer,  files को पढ़ने और उसमे लिखने,  साथ ही साथ file के अंदर उसकी Position को control करने के लिए Use किए जाते है 

यह program और file के बीच एक interface का कार्य करता है और program को file के साथ विभिन्न तरीकों से interact करने करने की अनुमति देता है।

Syntax:
FILE *pointer Name;

Example:
FILE *fpter;

ऊपर example में 

FILE:
C में FILE एक pre-defined structure है जो file I/O operations को handle करता है

*fptr:
fptr, FILE प्रकार के structure का एक pointer है जिसका प्रयोग files को access करने और उसमे हेरफेर करने हेतु किया जाता हैं क्योंकि यह file के current position की memory address को store करता है।

File handling Function in C in Hindi - C में File handling Function

C library में कई functions है जिसके द्वारा File को Create, Read, Write, Open और Close किया जाता है
ये निम्न है

File handling function
Sn.  Function Description
1 fopen() file open करना
2 fprint () file में data को write करना
3 fscanf() file से data को read करना
4 fputc() file में एक character को write करता है
5 fgetc() file से एक character को read करता है
6 fclose() file को close करता है
7 fseek() दिए गए position पर file pointer को set करता है
8 fputw() यह file में Integer को write करता है
9 fgetw() यह file में Integer को read करता है
10 ftell() यह current position को return करता है
11 rewind() यह file की शुरुआत में file pointer को set करता है

Opening a file in C in Hindi - C में एक File Open करना

C में File को Read, Write या Update करने के पहले उसे fopen() function का Use करके Open करना होता है।
 
Syntax:
FILE *fopen(const char *fileName, const char *mode);

ऊपर Syntax में 
*fileName:
यह File के नाम तथा उसके path (location) को बताता है यदि यदि File Same directory में है तब तो हमे केवल File का नाम बताना होगा और यदि File किसी अन्य स्थान में है तब हमे File का पूरा Path देना होता है
जैसे C://folder Name//fileName.txt

*mode:
यह File के mode को बताता है कि उसे किस mode में open करना

Example:
FILE *fptr;
fileptr = fopen("myfile.txt", "r");

C में File open करने के लिए fopen() function का Use करते समय निम्न Mode का Use करते है

Different file mode using with fopen()
Mode Description
"r" Text file को read mode में open करता है
"w" Text file को write mode में open करता है
"a" Text file को append mode में open करता है
"r+" Text file को read और write mode में open करता है
"w+" Text file को read और write mode में open करता है
"a+" Text file को read और append mode में open करता है
"rb" Binary file को read mode में open करता है
"wb" Binary file को write mode में open करता है
"ab" Binary file को append mode में open करता है
"rb+" Binary file को read और write दोनों mode में open करता है
"wb+" Write mode: binary file को Binary file को read और write दोनो mode में open करता है
"ab+" Binary file को read और append mode में open करता है

Program for opening a file
#include<stdio.h> 
#include<stdlib.h> 
int main() 
{
    FILE *fileptr;

    // Opening a file("aisect.txt") in read mode
    fileptr = fopen("aisect.txt", "r");

    if (fileptr == NULL) 
 {
        printf("The file is not opened.\n");
        return 1;
    }
    printf("Now file has opened successfully.\n");

    // Closing the file
    fclose(fileptr);
    return 0;
}

Output:
The file is not opened

Explanation:
ऊपर Program को Run कराने पर File Open नही हुआ क्योंकि यह source directory में available नही है इस स्थिति में हम दो कार्य कर सकते हैं
1) file का पूरा Path दे जहां पर file उपस्थित है 
जैसे fptr = fopen("C:\directoryName\aisect.txt", "w");

2) एक नए file को Create करे जैसे नीचे बताया गया है

Creating a file in C in Hindi - C में file का निर्माण करना

fopen() function n केवल file को Open करता है परंतु file उपस्थित न होने पर नई file का निर्माण भी करता है आप file निर्माण के दौरान विभिन्न mode(w, w+,wb,wb+,a, a+ ab और ab+) का Use कर सकते हैं।

Example:
FILE *fptr;
fileptr = fopen("myfile.txt", "w");

Program for creating a file
#include<stdio.h> 
#include<stdlib.h> 
int main() 
{
    FILE *fileptr;

    // Creating a file("myfile.txt") in write mode
    fileptr = fopen("my.txt", "w");

    if (fileptr == NULL) 
     {
        printf("The file is not opened.\n");
        return 1; // Exit the program with an error code
     }
   else
     {
  printf("Now file has been created successfully.\n");
     }
    return 0;
}
 
Output:
Now file has been created successfully.

Writing to the file in C in Hindi - C में File में Write करना

ऊपर हमने एक file (myfile.txt) का निर्माण किया है अब उसमें Data Enter करने के लिए या लिखने के लिए सबसे पहले उसे fopen() function का Use करके 'w' mode अर्थात write mode में open करते है तथा fprintf() function का Use करके उसमे Text enter करते है और अन्त में उसे Close कर देते हैं

Example:
FILE *fptr;
fileptr = fopen("myfile.txt", "w");
fprintf(fptr, "Hello students");

Program for writing to a file
#include<stdio.h> 
#include<stdlib.h>
int main() 
{
  FILE *fptr;

  // Opening a file in write mode
  fptr = fopen("myfile.txt", "w");

// Check if file opening was successful
     if (fptr == NULL)
       {
        printf("File is not opening .\n");
        return 1;
       }

// Enter some text to the file
  fprintf(fptr, "Hello students");

  // Closing the file
  fclose(fptr);
  return 0;
}

Output:
Hello students

Explanation:
जैसे आप ऊपर का Program लिखकर उसे Run कराते है आप देखेंगे कि "Hello students" contents आपके File में Write हो चुका है आप इसे देखने के लिए C program की Bin folder में जाकर अपनी file (myfile.txt) को खोले जो एक Notepad file है

Append contents to a file in Hindi - C में एक file में contents, append (जोड़ना) करना

यदि File में पहले से contents मौजुद है और हम और contents Add करना चाहते है लेकिन पहले के contents को भी हटाना नही चाहते तो इसके किए हम 'a' mode का Use करेंगे तथा file के आखरी मे contents Add करेंगे
Example:
FILE *fptr;
fileptr = fopen("myfile.txt", "a");

Program to append contents to a file
#include<stdio.h>
#include<stdlib.h> 
int main() 
{
  FILE *fptr;
  // Opening a file in append mode
  fptr = fopen("myfile.txt", "a");

  // Append some text to the file
  fprintf(fptr, "\n Good Morning");

  // Close the file
  fclose(fptr);
  return 0;
}

Output:
Hello students
Good Morning

Explaination:
शुरुवात में File में "Hello students" पहले से लिखा गया था अब जब हम ऊपर के Program को Run करते है तब उसमें "Good Morning" contents और Append(जुड़) हो जाता है और हमे Output के रूप में 
Hello students
Good Morning 
मिलता है

Read from file in C - C में file को read करना

हमने w और a mode का Use करके अपनी file में write किया और बाद में कुछ Text append किया अब हम अपनी file को read करना चाहते है इसके लिए 'r' mode का Use करेंगे

Example:
FILE *fptr;
fileptr = fopen("myfile.txt", "r");

Program to read from file
#include<stdio.h> 
#include<stdlib.h>
int main() 
{
  FILE *fptr;

  // Opening a file in read mode
  fptr = fopen("myfile.txt", "r");

  char myStr[100];

 // Reading  the content and store it inside myStr
  fgets(myStr, 100, fptr);

  // Print file content
  printf("%s", myStr);  
  
  // Closing the file
  fclose(fptr);

  return 0;
}


 Related Post