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. --...
UNIX in Hindi UNIX एक multi tasking, multi user operating system  है जिसे सन 1969  में AT & T LABS  में बनाया गया था। जिसे AT & T में काम करने वाले computer  scientist  ken Thompson  &   Denis Ritchie and  उनके friends ने मिलकर  बनाया गया था.  ऐसे AT & T   ने अपने उसे के लिए बनाया गया था लेकिन बाद में सन 1970 में इसे commercialize कर दिया गया था।  इस Operating System को ख़ास तौर पर Programmers और developers के लिए बनाया गया था। इसकी बजह यह थी की यह Modular Programming Interface design provide करता था जिसे "Unix Philosophy" भी कहा जाता है। UNIX Operating System को 'C' और Assembly Programming Languages में लिखा गया था। C Programming language को खास तौर पर UNIX बनाने के लिए ही develop किया गया था और इसे भी Denis Ritchie द्वारा ही बनाया गया था। आज भी 'C' Programming Language को बहुत ख़ास माना जाता है क्यूंकि Device Drivers इसी language में ज्यादातर लिखे जाते हैं और साथ ही इसे  Mother...
Addressing modes of microprocessor 8085 The way of specifying data to be operated by an instruction is called addressing Types of addressing modes – In 8085 microprocessor there are 5 types of addressing modes: Immediate Addressing Mode – In immediate addressing mode the source operand is always data. If the data is 8-bit, then the instruction will be of 2 bytes, if the data is of 16-bit then the instruction will be of 3 bytes. Examples: MVI B 45 (move the data 45H immediately to register B) LXI H 3050 (load the H-L pair with the operand 3050H immediately) JMP address (jump to the operand address immediately). Register Addressing Mode – In register addressing mode, the data to be operated is available inside the register(s) and register(s) is(are) operands. Therefore the operation is performed within various registers of the microprocessor. Examples: MOV A, B (move the contents of register B to register A) ADD B (add contents of registers A and B and s...