File Handling in C++ in hindi

In the concept of file handling in C++ we will discuss following,


File handling in C++

सरल शब्दों में, file handling में, हम एक program के माध्यम से एक file create करते हैं, जो internal storage जैसे hard -disk में store होती हैं और इस file में हम end-user द्वारा दी गई जानकारी store करते हैं।

एक तरह से, आप कह सकते हैं कि यह file C ++ में database के रूप में कार्य करती है। इस file में हम user-input को store कराते हैं जो program के terminate होने के बाद भी internal storage में store रहती है और साथ ही इस data को program re -execute करके access किया जा सकता है तथा उसे manipulate भी किया जा सकता है जैसे modify , delete , search आदि।

आइये इसे विस्तार से समझते हैं-

why we use file-handling? benefit of file-handling

जैसा कि हम जानते हैं कि एक program में, हम user द्वारा दी गई जानकारी को एक variable में store करते हैं और variable इन information को memory में store करता है। लेकिन यह information memory में केवल तब तक store रहती हैं, जब तक कि program execution में रहता है जैसे ही program terminate होता है यह information भी remove हो जाती हैं।

लेकिन अगर हम चाहते हैं कि program terminate होने के बाद भी end – user द्वारा information , memory में store रहें। तो इसके लिए C ++ में हम file handling को use करते हैं।

writing and reading a file

किसी Program में file handling का उपयोग करने से पहले, हमें इसकी header file को program में include करना होता है।

fstream.h

जैसे अन्य program में output के लिए cout और input के लिए cin statement का use किया जाता है

वैसे ही , file handling में दो stream-object(sequence of byte) होते हैं, जिनसे किसी file में stored-data के output के लिए output-stream और file में data input करने के लिए input -stream का प्रयोग किया जाता है।

 

लेकिन याद रखें जिसमें जहाँ cout और cin, reserve keyword हैं, वहीँ output और input-stream , दोनों identifier होंगे। अर्थात कोई भी उपयुक्त नाम।

Implementation of a file in a program

किसी program में file use के लिए हमें कुछ बातों को ध्यान में रखना होता है –

  • file का नाम और इसका extension type
  • C++ stream class
  • Closing the file

File name and its extension type

filename कोई भी एक उपयुक्त नाम हो सकता है, अर्थात, user defined name यहाँ पर file extension optional होता है।

जैसे कि आप नीचे के diagram में देख सकते हैं –

file handling concept in C++

लेकिन अगर आप extension देते हैं, तो program उस प्रकार की create करेगा। जिस प्रकार का file extension type होगा जैसे कि

student.txt // text file will be created
student.pdf // PDF file will be create
student.doc // Ms word file will be created 
student // normal file will be created, which will be opened in notepad.

C++ stream class

जब हम file कोई use में लाते हैं, तो सबसे पहले हमें  stream class को define करते हैं। यह stream class बताता है कि file को किस उद्देश्य से use किया गया है।

यहाँ कुछ stream-class और उनके purpose दिए गए हैं-

stream class Task
ofstream file को केवल write करने के लिए ,
ifstream file को केवल read करने के लिए ,
fstream file को एक साथ write और read करने के लिए ,

Open a file in Two ways

  • एक program में, हम एक file को दो तरह से open कर सकते हैं जैसे- एक single statement में ,
stream-class stream-object(filename);
  • इसमें हम पहले stream -class को stream-object के साथ declare करते हैं, फिर next statement में उसी stream -object के साथ एक file open करते हैं –
stream-class.stream-object;
stream-object.open(filename);

दूसरे विकल्प का use तब किया जाता है जब हम एक ही stream object से कई files open करते हैं, जबकि single file के लिए पहला विकल्प का use किया जाएगा।

: जैसे ही हम file की open statement लिखते हैं, file अपने आप create हो जाती है, इसलिए file create करने के लिए अलग से कोई statement नहीं होती।

Closing a file

file close करने के लिए, close() function का उपयोग stream object के साथ किया जाता है

stream-object.close();

stream-object byte का sequence of bytes होता है, जो internal storage से data fetch करने के लिए उपयोग किया जाता है।

How to store records in internal storage from a program

The below diagram, you can see that the data inputted from the keyboard is displayed on the monitor screen, from the cout statement, and in file handling, the data is stored in the internal storage from the stream-object.

file-handling-in-cpp

 

ध्यान दें, यहाँ cout statement केवल monitor पर data को display करता है जबकि stream-object internal storage (किसी file में ) में data को store करेगा। दोनों अलग हैं।

write a single file in C++

यहां हम एक single file को insertion (<<) से write करते हैं जबकि extraction operator से write किये data को display कराते हैं।

Example

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>           //for gets()

void main() // main function
{

/*writing file into database file*/

    char name[20];
    clrscr();
    ofstream fout("database.txt"); // open database file for writing only

    cout<<"Enter Name: ";
    gets(name);
    fout<<name;  // write to file
    fout.close();

/*reading record from database file*/

    ifstream fin("database.txt"); // open database file for reading only

    cout<<"Displaying record from file: ";
    fin>>name;  // fetch data from file
    cout<<name;    //display 
    fin.close();

getch();
}

OUTPUT

Enter name: Rahul_sherma
Displaying record from file: Rahul_sherma

C ++ में , file को write और read करने के अन्य तरीके भी हैं। इनमें से कुछ इस प्रकार हैं-

Three way to write and read a file

  • write file using insertion and extraction operator
  • write file using put() and get(),
  • write file using write() and read()

write and read a single file using put and get function

put(), file को write करेगा जबकि get() data को read करेगा।

इसका syntax नीचे दिया गया है –

SYNTEX

stream_object.put(character); // to write a file
stream_object.get(character);// to read a file

जैसे की syntax से पता चलता है दोनों function , task को एक single character लेकर process करेंगे। इसका program नीचे दिया गया है –

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h> // for strlen();

void main()
{

/*writing file*/
      char name[45]; int len;
      clrscr();

      ofstream fout("database.txt");
      cout<<"Enter Name: ";
      gets(name);

      len = strlen(name); // calculate length of name

      for(int i=0; i<len; i++)
      {
         fout.put(name[i]); // write file one by one character using put()
       }
      fout.close();

/*reading file*/
       char ch;//int size=30;
       ifstream fin("database.txt");
       cout<<"Displaying record from file: ";

       while(!fin.eof()) // end of file
       {
         fin.get(ch);  // read file one by one character
        // or fin.getline(name,size); // read file using a line depend on size

         cout<<name;
       }
       fin.close();

getch();
}

OUTPUT

Enter name: Rahul_sherma
Displaying record from file: Rahul_sherma

file handling Parameters

file handling में, कुछ parameter दिए गए हैं जिससे हम file related task perform कर सकते हैं जैसे , file में large volume data store करना, file का storage type निर्धारित करना और भी अन्य।

उदाहरण के लिए,

अब तक, आप उपरोक्त उदाहरण से जानते होंगे, कि file में data केवल एक बार ही store कर रहा है अर्थात प्रत्येक नए execution में, program नए डेटा को store कर रहा है और पिछले execution में input किये data को remove कर दे रहा है। जिससे हम file में एक large volume data को store नहीं कर पा रहे हैं।

यहाँ कुछ parameters दिए गए हैं –

file को एक साथ write और read करने के लिए ,

Parameter name Task
ios::out file को केवल write करने के लिए ,
ios::in file को केवल read करने के लिए ,
ios::app file में large volume of data को store करने के लिए
ios::ate file के end में जाने के लिए
ios::nocreate file की पहले से exist है या नहीं , check करने के लिए
ios::trunc file के data को erase करने के लिए
ios::binary file में , data को binary form में store करने के लिए।

किसी program में , parameter use करने का syntax नीचे दिया गया है –

stream-class stream-object("file-name",parameter);

याद रखें, file के writing और reading parameter , उनके stream -object के साथ by -default होते हैं ,आप इसे ऊपर के program में देख सकते हैं। जिसमें हमने किसी भी प्रकार के parameter का उपयोग नहीं किया है, फिर भी file को write और read किया गया है।

Store Large Volume of data in a file

इस program में , ios::app parameter का प्रयोग किया गया है।

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
#include<stdio.h>

void main() 
{
/*writing record into file */
   char name[45];
   clrscr();

   ofstream fout("data.doc",ios::app); // using app(append) mode to store data multiple execution
   
    cout<<"Enter Name: ";
    gets(name);
    fout<<name;
    fout.close();

/*reading record from file*/
    const int size=80; char ch[50];
    ifstream fin("data.doc");

    cout<<"Displaying record from file: \n";
    fin>>name;
    cout<<name<<" ";
    fin.close();

   getch();
}

OUTPUT

1st Execution

Enter name: Rahul_sherma
Displaying record from file: Rahul_sherma

2nd Exection

Enter name: rakesh_sherma
Displaying record from file: Rahul_sherma rakesh_sherma

Explanation

यहां हमने program को दो बार execute किया है

1execution में , एक नाम “rahul singh ” store किया गया है

2nd execution में , फिर एक different name “anand singh ” store किया गया है। यहाँ पर आप देख सकते है हमें पिछला data भी प्राप्त हुआ है।

इस प्रकार हम एक file में parameter ios::app से large volume data को store कर सकते हैं।

manipulators in file handling

C ++ में, file के लिए कुछ manipulators दिए गए हैं, जिनके उपयोग से हम जैसे – file size , file को किसी specific location से write करना, operation कर सकते हैं।

इन manipulators नीचे table में बताया गया है –

get pointer Task
seekg(); file को किसी विशेष जगह से read करने के लिए
tellg(); input -stream को current position देने के लिए
put pointer
seekp(); file को किसी विशेष जगह से write करने के लिए
tellp(); output -stream को current position देने के लिए
Parameter
ios::beg file में , data के beginning point पर जाने के लिए
ios::cur file में , data के current location पर जाने के लिए
ios::end file में , data के end पर जाने के लिए

 

याद रखें, seekg(), tellg() input stream के साथ इस्तेमाल किया जाएगा, जबकि seekp(), tellp() का उपयोग output -stream के साथ।

आइए इसे उदाहरण से समझते हैं,

उदाहरण के लिए, यदि हमने एक file में 50 student को store किया है, तो file में इन record को calculate करने के लिए –

istream fin;
fin.open("file-name", );
fin.seekg(0,ios,::end);
int total = fin.tellg()/sizeof (class-name);

ऊपर किसी file को write और read करने के तीन तरीके के बारे में बताया गया है जिसमे दो के बारे में पहले ही बताया गया है तीसरे का syntax नीचे दिया गया है –

steam-class.stream-object;
stream-object.open(filename,mode);

write करने के लिए

stream-object.write((char *)&variable, sizeof(variable);

read करने के लिए

stream-object.read((char *)&variable, sizeof(variable);

या,

stream-object.write((char *)&class-object, sizeof(class-name);

जिसमें पहले parameter में object का address होता है जबकि दूसरे parameter में object का size

यहाँ इसका उदाहरण दिया गया है जो student record store करता है।

इसमें आपको कुछ इस तरह के selection-menu प्राप्त होगा –

Insert record
Display record
Search record
Modify record
Delete record
Properties
Exit

user-defined filename, remove a file, rename a file

C ++ में किसी अन्य operator का उपयोग करके हम किसी file के लिए deletion और rename जैसे task perform कर सकते हैं। ये function file handling से सम्बंधित नहीं हैं।

यहाँ इनका syntax दिया गया है –

rename (old-file-name, new-file-name);
remove (filename);

क्योंकि ये function , file handling से सम्बंधित नहीं है इसलिए हमें program में इनकी header file (stdio)को include करना होगा।

इनका use नीचे program में किया गया है –

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>  // rename, remove

void main() 
 {
   char name[20],filename[20],newname[20];
   clrscr();

/*create filename*/
    cout<<"Enter file-name to create: ";
    gets(filename);
    ofstream fout(filename);

/*save record into file*/
    cout<<"\nEnter a name to store in it: ";
    gets(name);
    fout<<name;
    fout.close();
    cout<<"\nrecord saved into file called - "<<filename<<endl;

/*rename file*/
    cout<<"\nHit to Rename this file";
    getch();
    cout<<"\nEnter new name: ";
    gets(newname);
    rename(filename,newname);
    cout<<"\nNow file name is: "<<newname<<endl;

/*remove file*/   
    cout<<"\nHit to remove file\n";
    getch();
    remove(newname);
    cout<<"\nfile removed successfully...";
    getch();
 }

OUTPUT

Enter file-name to create: database

Enter a name to store in it: rahul

record saved into file called - database

HIT ENTER to Rename this file
Enter new name: file

Now file name is: file

HIT ENTER to Remove file

file removed successfully...

यदि हम binary parameter (ios::bin ) का उपयोग करके data store करते हैं, तो उस file में stored data binary format का होगा जिसे अन्य files की तुलना में अधिक तेज़ी से access किया जा सकता है और साथ ही इसका size भी कम होता है।

किसी file में data को, binary format और character format में, दो तरीके से store किया जाता है जहाँ character format by default होता है। इसके बारे में और अधिक जानने के लिए इसे देखे।

text format and binary format in C++ in hindi

Related exercise

Leave a Reply

Your email address will not be published.