C File Handling

Introduction to C File Handling

एक file bytes की sequence होती है। File किसी भी data को store करने के लिए यूज़ की जाती है। Files को permanent storage के लिए यूज़ किया जाता है। जब आप किसी program को run करवाते है और उसका output देखने के बाद उसे बंद कर देते है तो उसका output वहीँ remove हो जाता है।

यदि आप इस output को permanently store करना चाहते है तो C language के file handling feature को यूज़ कर सकते है।

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 किया था।