Skip to main content
C file handling का यूज़ यँही ख़त्म नहीं होता है। यदि program में दिए जाने वाला input भी बहुत अधिक है तो किसी human द्वारा इसे enter कराने के बजाय आप file का यूज़ कर सकते है। इससे program automatically file से ही input ले लेगा।
C language के द्वारा आप files से related ये काम कर सकते है।
  1. Existing files को open कर सकते है और नयी files create कर सकते है। 
  2. किसी भी program के output को file में store कर सकते है। 
  3. File के द्वारा किसी भी program में input भी दे सकते है।       
  4. सभी operations perform होने के बाद files को close कर सकते है।      

Opening a File 

किसी भी file में output store करने या उसमें से input लेने से पहले उसे open किया जाता है। File open करने के लिए आप fopen() function को use करते है। 
जब आप किसी ऐसी file को open करने के कोशिश करते है जो exist ही नहीं करती तो वो file automatically create हो जाती है। यदि file पहले से exist करती है तो वह open हो जाती है। आप किस mode में file को open करते है ये उस पर depend करता है।
C language के द्वारा आप files को अलग अलग modes में open कर सकते है। ये mode आप function को call करते समय define करते है। उदाहरण के लिए यदि आप किसी file से सिर्फ data read करने वाले है तो आप उसको read mode में open कर सकते है।
और यदि आप file में output store करना चाहते है तो उसे write mode में open कर सकते है। और आप चाहे तो किसी file को दोनों modes में भी open कर सकते है। 

FILE OPENING MODES 

Mode 
Description 
इस mode से आप एक existing file को open कर सकते है। इस mode में आप files को सिर्फ read कर सकते है।    
w
इस mode से आप सिर्फ files में write कर सकते है। यदि file पहले से exist नहीं करती है तो वह create हो जाती है।   
इस mode से आप output write करते है। इस mode में open की गयी file यदि exist करती है तो वह open हो जाती है और उसमें जो data होता है उसी के बाद से नया data add हो जाता है। यदि file exist नहीं करती है तो नयी file create हो जाती है।   
r+ 
इस mode में आप एक file को reading और writing दोनों purposes के लिए open कर सकते है।  
w+ 
इस mode में file reading और writing दोनों purposes के लिए open की जाती है। Write करने के लिए इस mode में पहले file zero length तक truncate की जाती है।   
a+ 
इस mode में file reading और writing दोनों modes में open होती है। यदि file exist नहीं करती तो create हो जाती है। और writing हमेशा existing data के बाद शुरू होती है।    
fopen() function में 2 arguments pass किये जाते है।
FILE *fopen(const char * file_name, const char * mode );
एक तो hard disk पर file का address और दूसरा वह mode जिसमें आप file open करना चाहते है। ये function FILE type का एक object return करता है। इसलिए इसका return type FILE होता है।
और इसीलिए आप इस function के द्वारा return किये गए object को FILE type के दूसरे object में store करेंगे।

Reading From a File 

C language के द्वारा किसी file को read करने के लिए आप 2 functions यूज़ कर सकते है।
  1. fgetc()
  2. fgets()
fgetc() function एक बार में एक ही character read करता है लेकिन इसे आप loop में यूज़ करके पूरी file को read कर सकते है। इस function में open की गयी file के pointer के रूप में एक ही argument pass किया जाता है।
#include<stdio.h>
int main()
{
    /* File pointer variable to store file location */
    FILE *fp;
    char data[1];
    /* Opening input.txt in read mode and assigning to file pointer variable */
    fp = fopen(“input.txt”,”r”);
    /*  Reading from file using fgetc() and storing data in data array */
    data=fgetc(fp);
    printf(“%s”,data[]);
    /* Closing file stream */
    fclose(fp);    
    return 0;
}
उपर दिए गए उदाहरण में fgetc() function का यूज़ करते हुए file में से एक character read किया गया है। ये program input.txt file से एक character read करता है। यदि file exist नहीं करती है तो run time error generate होती है।
fgets function से आप define किये गए characters तक file को read कर सकते है। इस function में 3 arguments pass किये जाते है।
पहला argument एक character array होता है। ये वो array होता है जिसमे read किया गया data store किया जाता है।
दूसरा argument जितने characters आप read करना चाहते है उनकी size होती है। तीसरा argument file pointer होता है।
#include<stdio.h>
int main()
{
    /* File pointer declaration */
    FILE *fp;
    char data[100];
 
    /* Opening file with read permission */
    fp=fopen(“test.txt”,”r”);
    /* Reading file using fgets() and assigning to data array */ 
    fgets(data,100,fp);
    printf(“%s”,data);
    fclose(fp);
    return 0;
}    
ऊपर दिया गया program test.txt file से 100 characters read करता है। यदि file पहले से exist नहीं करती है तो run time error generate होती है।

Writing to a File

C language के द्वारा किसी file में data write करने के लिए आप 2 functions यूज़ कर सकते है। 
  1. fputc()
  2. fputs()
यँहा पर fputs() function का example नीचे दिया जा रहा है। इस function में 2 arguments pass किये जाते है। पहला argument वो string होती है जिसे आप file में store करना चाहते है और दूसरा argument file pointer होता है।
#include <stdio.h>
int main()
{
    FILE *fp;
    /* Opening file with read and write permissions */
    fp = fopen(“test.txt”,”w+”);
    /* Writing to file */ 
    fputs(“This is text string”,fp);
    fclose(fp); 
    return 0;
}
ऊपर दिया गया program test.txt file में This is a string write करता है।

Closing a File

File को close करने के लिए आप fclose() function को यूज़ करते है। 
int fclose(FILE *fp);
उपर दिए गए function में *fp वो FILE type object है जिसमे आपने fopen() function के द्वारा return किये गए object को store किया था।
Posted

Comments

Popular posts from this blog

    METACHAR ACTER   UNIX  shell  अनेक मेटाचारेक्टर प्रोवाइड करता है जो किसी भी शैल स्कफ्रिप्त में उनका उसे करते टाइम विषेस माइनिंग रखते और जब तक काशी सोवळे नहीं हो जाते तब काट किसी वर्ड का एंडिंग का कारन बनते है।                                                     ex:--     एक directory में फिलो को लिस्टिंग करते टाइम एक सिंगल चैरेक्टर में रखा जाता है और * एक से अधिक वर्ड को मैचेस करता है यह शैल के अधिकांश पत्रों को लिस्ट दी गयी है जिन्हे metacharacter बोला  जाता है.   * ? [ ] " ' \    /    $ : ; ( ) | ^ < >  \.   नई लाइन स्पेस तब   #!/bin/sh  echo hello; word  #!/bin/sh  echo "I have \$1200" 1. <  single quotes : all special charecters between these quotes lose their special.  ex. --...
Until loop  The  until  loop is very similar to the  while  loop, except that the loop executes until the  TEST-COMMAND  executes successfully. As long as this command fails, the loop continues. The syntax is the same as for the  while  loop: until TEST-COMMAND; do CONSEQUENT-COMMANDS; done The return status is the exit status of the last command executed in the  CONSEQUENT-COMMANDS  list, or zero if none was executed. TEST-COMMAND  can, again, be any command that can exit with a success or failure status, and  CONSEQUENT-COMMANDS  can be any UNIX command, script or shell construct. Example :--            Write a shell script to display first ten positive numbers using until loop.                                     Program num=1 until [ $num –gt 10 ]; do echo num ...