C++ File Handling

Introduction to C++ File Handling

C++ के द्वारा files को भी handle किया जा सकता है। इसके लिए C++ आपको streams provide करती है। Basically C++ में आप files के साथ नीचे दिए गए tasks perform कर सकते है।

  1. Opening a file
  2. Reading from a file
  3. Writing to a file
  4. Closing a file

C++ में file handling support provide किये जाने का मुख्य कारण output को permanently store करना है। Normally जब भी आप कोई program run करते है तो उसका output monitor पर show होता है। जैसे ही आप output console window से exit होते है तो आपके program का output भी lost हो जाता है।

ऐसी situation में एक ऐसे mechanism की requirement थी जिससे की output को permanently hard disk पर किसी file में store किया जा सके और जरुरत पड़ने पर उसे वापस retrieve किया जा सके।

C++ में file handling provide किये जाने का दूसरा मुख्य कारण output को दुबारा यूज़ करना है। एक बार जब आप output को file में store करवा ले तो उसे future में वापस भी यूज़ कर सकते है। उदाहरण के लिए आप इसे किसी दूसरे program में input के रूप में यूज़ कर सकते है।

आगे file handling में यूज़ होने वाली stream classes के बारे में बताया जा रहा है। यदि आपको streams का concept clear नहीं है तो आप C++ I/O System tutorial में पढ़ सकते है।

C++ File Stream Classes

File operations perform करने के लिए C++ आपको 3 stream classes provide करती है। आइये इनके बारे में जानने का प्रयास करते है।

ifstream Classs

इस class के objects input file stream को represent करते है और files से input लेने के लिए यूज़ किये जाते है।

ofstream Class

इस class के objects file output stream को represent करते है। इस class के द्वारा आप files create करते है और files में output भी write करते है।

fstream Class

इस class के objects complete file stream को represent करते है। इस class के objects से आप file से input read भी कर सकते है और files में output write भी कर सकते है।

Opening a File

C++ में files को open करने के लिए आप open() method यूज़ करते है। इस method को ऊपर किसी भी file stream class के object पर call किया जा सकता है। इस method का general syntax नीचे दिया जा रहा है।

void open(const char *filename, ios::mode);

जैसा की आप ऊपर दिए गए syntax में देख सकते है, open() method 2 arguments लेता है। पहला argument एक constant character type का file name होता है। Basically ये एक pointer होता है जो memory में file को point करता है। पहले argument के रूप में आप कोई भी variable नहीं दे सकते है।

File open करने के लिए fopen() method में दूसरा argument आप file opening mode pass करते है। इन modes के बारे में आगे detail से समझाया जा रहा है।

File Opening Modes

किसी भी file से आप input read करें या उसमें output write करे सबसे पहले उसे properly open करना जरुरी है। Files को अलग अलग purposes के लिए open किया जाता है। इसलिए C++ आपको file open करने के लिए अलग अलग modes provide करती है। इन्हें file opening modes कहा जाता है।

इन modes का use files की safety के लिए होता है। इन modes के द्वारा आप authentication provide करते है की कौनसा program सिर्फ file को read कर सकता है और कौनसा program file से read भी कर सकता है, write भी कर सकता है और modify भी कर सकता है।

नीचे file opening modes की list दी जा रही है। इन modes को आपको files को open करने के लिए define करना होता है।

ModeExplanation
ios :: in ये mode file को read only permission के साथ open करता है। इस mode में open की गयी files में आप कुछ भी write नहीं कर सकते है। ifstream class के objects के लिए ये default mode होता है।     
ios :: out  इस mode से file write only permission के साथ open की जाती है। इस mode से open की गयी files से आप input read नहीं कर सकते है। ofstream class के objects के लिए ये default mode होता है।     
ios :: app ये mode existing file के end में output append करने के लिए यूज़ किया जाता है। इसे append mode कहते है।    
ios :: ate ये mode output के लिए open किया जाता है। इस mode में file open होते ही control file के आखिर में पहुच जाता है।    
ios :: trunc  इस mode में open की गयी file का पूरा content delete हो जाता है। यदि आप file के existing content को delete करके उसमें नया data भरना चाहते है तो आप ये mode यूज़ कर सकते है।

अभी तक आपने open() method के syntax के बारे में जाना है। आइये अब इसे एक उदाहरण से समझने का प्रयास करते है।

ifstream obj;
obj.open("MyFile.txt",ios::app);

ऊपर दिए गए उदाहरण में ifstream class के object पर open method को call किया गया है। Method में file का नाम और appending mode pass किया गया है।

आप चाहे तो बिना opening mode pass किये भी file को इस प्रकार open कर सकते है।

ifstream obj;
obj.open("MyFile.txt")

जब आप कोई भी opening mode pass नहीं करते है तो object की class का default mode यूज़ किया जाता है। जैसा की में पहले बता चूका हूँ ifstream class का default mode ios::in है और ofstream class का default mode ios :: out है। इसके अलावा fstream class के लिए ये दोनों ही default mode होते है। ifstream class के object द्वारा आप out mode में और ofstream class के object द्वारा आप in mode में file open नहीं कर सकते है।

Writing to a File

Files में output write करने के लिए आप ofstream class के object को यूज़ करते है। इस object को left shift operator («) के साथ यूज़ किया जाता है। इसका general syntax नीचे दिया जा रहा है।

ofstream-object<<variable-name;

आइये इसे उदाहरण से समझने का प्रयास करते है।

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ofstream fobj;
    fobj.open("MyFile");   //ofstream class object

    cout<<"Enter your name : ";
    char yrname[30];
    cin.getline(yrname,30);  //Taking input from user

    cout<<"Writing your name to file";  
    fobj<<yrname;   //Writing name into file
    cout<<"Your name was written into file...";
    fobj.close();
    return 0;
}

ऊपर दिया गया program निचे दिया गया output generate करता है।

Enter your name :
Best Hindi Tutorials
Writing your name to file...
Your name was written into file...

Reading From a File

Files में से input read करने के लिए आप ifstream class के object को use करते है। इस object को overloaded right shift operator (») के साथ यूज़ किया जाता है। इसका general syntax नीचे दिया जा रहा है।

ifstream-obj >>variable-name;

इसे नीचे उदाहरण के माध्यम से समझाया जा रहा है। इस उदाहरण में MyFile से वापस name को read किया जा रहा है। इससे पहले ऊपर दिए गए उदाहरण में MyFile में name को write किया गया था।

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
     ifstream ifobj;
     char yrname[30];
     ifobj.open("MyFile");
     cout<<"Reading data from file...";
     ifobj>>yrname;

     cout<<yrname;
     ifobj.close()
     return 0;
}

ऊपर दिया गया program निचे दिया गया output generate करता है।

Reading data from file...
Best Hindi Tutorials

Closing a File

एक बार जब आप file के साथ read/write operation perform कर ले तो आप उस stream को close करते है। इसके लिए आप close() method यूज़ करेंगे। इस method को आप stream object पर call करते है। इसका general syntax नीचे दिया जा रहा है।

stream-class-object.close();

C++ File Pointers

जब भी आप C++ के द्वारा किसी file को read और write करने के लिए open करते है तो उसके साथ 2 तरह के pointers associated रहते है। ये pointers file में start से end तक कँही भी move कर सकते है।

  1. Get pointer - इसे input pointer भी कहा जाता है। ये pointer file में किसी particular location से input read करने के लिए यूज़ किया जाता है। जब भी आप file को read only mode में open करते है तो by default get pointer file के beginning में होता है।
  2. Put pointer - इसे output pointer भी कहा जाता है। इस pointer से आप file में एक particular location पर output store कर सकते है। जब भी आप file को write only mode में open करते है तो file का content delete हो जाता है और by default put pointer file के beginning में होता है।

Note : जब आप किसी file को append mode में open करते है तो put pointer existing content के आखिर में होता है।

Manipulating File Pointers

Pointers को उनकी default positions से move किया जा सकता है। इसके लिए C++ आपको 4 functions provide करती है। इन functions को stream class objects पर call किया जाता है।

  1. seekg() - ये function get pointer को argument के रूप में specify की गयी location पर move करता है। Argument में आप byte number pass करते है। इसे execute होने के बाद उसी byte location से input read किया जा सकता है।
  2. seekp() - ये function put pointer को specify की गयी location पर move करता है। इस function में भी argument के रूप में byte number pass किया जाता है। इसे execute होने के बाद आप उसी location से output store करना प्रारम्भ कर सकते है।
  3. tellg() - ये function get pointer की current position return करता है।
  4. tellp() - ये function put pointer की current location return करता है।

Example:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream ifobj;
    ifobj.open("MyFile",);       //Read Only

    ofstream ofobj;
    ofobj.open("MyFile",ios::app)    //Append mode

    cout<<"Current position of input file pointer : "<<ifobj.tellg()<<endl;          
    cout<<"Going to position 10th "<<endl;
    ifobj.seekg(10);    
    cout<<"Current position of input file pointer : "<<ifobj.tell()<<endl<<endl;
    cout<<"Current position of input file pointer : "<<ofobj.tellp()<<endl;
    cout<<"Going to 15th position "<<endl;           
    cout<<"Current position of output file pointer : "<<ofobj.seekp(15)<<endl;  

    return 0;
}  

ऊपर दिया गया program निचे दिया गया output generate करता है।

Current position of input file pointer : -1
Going to position 10th
Current position of input file pointer : 9

Current position of output file pointer : -1
Going to 15th position :
Current position of output file pointer : 14