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

Programming model of microprocessor 8085 prograaming modal  chhah saamaany prayojan rajistar 1.8085 mein chhah saamaany prayojan rajistar hote hain jaise b, ch, d, ai, h, l. 2. mote rajistar ko 16-bit opareshan (beesee, de, echel) karane ke lie rajistar jode ke roop mein joda ja sakata hai. sanchayakarta rajistar 1. yah akshar a dvaara pahachaana jaata hai. 2. yah alu ka ek hissa hai. 3. isamen 8-bit deta storej hai. 4. yah ankaganit aur taarkik sanchaalan karata hai. 5. kisee opareshan ka parinaam sanchaayak mein sangraheet kiya jaata hai. dhvaj panjee 1. yah bhee alu ka ek hissa hai 2.8085 mein paanch jhandon ke naam hain -jiro jhanda (z) -kaary dhvaj (chy) -sain phlaig (es) -parivartan dhvaj (p) sahaayak kairee phlaig (esee) 3. in jhande dhvaj rajistar mein paanch phlip-phlop hain. 4. ek ankaganit / tark sanchaalan ka nishpaadan in jhandon ko set ya reeset kar sakata hai. 5. sophtaveyar nirdeshon ke maadhyam se jhande (set ya reeset) ka pareeks...
Java in hindi : Introduction    History of Java  Java एक general purpose object oriented programming लैंग्वेज है। Sun Micro-systems ने java 1991 में U.S. में बनायीं थी। Java का नाम पहले oak रखा गया था लेकिन बाद में इसे change करके java किया गया था। Java James Gosling ने develop की थी। Java को consumer electronics जैसे की TV, VCR आदि software बनाने के लिए develop किया गया था।  Java का सबसे important और popular feature था की java platform independent थी। क्योंकि java किसी एक particular hardware या operating system के लिए नहीं बनायीं गयी थी। Java में बनाये हुए program किसी भी system पर execute किये जा सकते है। Java का ये feature java को आज भी सबसे popular लैंग्वेज बनाता है।  Java version  . ЈDK  Alpha and Beta (1995) .JDK 1.0 (23 Jan  , 1996) .  JDK 1.1(19 Fe, 1997) .   J2SE 1.2 (8 Dese, 1998)  .  J2SE 1.3 (8 May, 2000)  .  J2SE 1.4 (6 Feb, 2002)  .  J2SE 5.0 (30 Feb, 2...