- Introduction to C++ file handling in Hindi
- Classes used for C++ file handling in Hindi
- Functions used for C++ file handling in Hindi
Introduction to C++ File Handling
C++ के द्वारा files को भी handle किया जा सकता है। इसके लिए C++ आपको streams provide करती है। Basically C++ में आप files के साथ नीचे दिए गए tasks perform कर सकते है।
ऐसी 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 में पढ़ सकते है।
अभी तक आपने open() method के syntax के बारे में जाना है। आइये अब इसे एक उदाहरण से समझने का प्रयास करते है।
ऊपर दिए गए उदाहरण में ifstream class के object पर open method को call किया गया है। Method में file का नाम और appending mode pass किया गया है।
आप चाहे तो बिना opening mode pass किये भी file को इस प्रकार open कर सकते है।
जब आप कोई भी 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 नहीं कर सकते है।
ऊपर दिया गया program निचे दिया गया output generate करता है।
इसे नीचे उदाहरण के माध्यम से समझाया जा रहा है। इस उदाहरण में MyFile से वापस name को read किया जा रहा है। इससे पहले ऊपर दिए गए उदाहरण में MyFile में name को write किया गया था।
ऊपर दिया गया program निचे दिया गया output generate करता है।
- Opening a file
- Reading from a file
- Writing to a file
- Closing a file
ऐसी 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 नीचे दिया जा रहा है।
जैसा की आप ऊपर दिए गए 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 से समझाया जा रहा है।
जैसा की आप ऊपर दिए गए 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 करना होता है।
ifstream obj;
obj.open("MyFile.txt",ios::app);
|
ऊपर दिए गए उदाहरण में ifstream class के object पर open method को call किया गया है। Method में file का नाम और appending mode pass किया गया है।
आप चाहे तो बिना opening mode pass किये भी file को इस प्रकार open कर सकते है।
जब आप कोई भी 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 नीचे दिया जा रहा है।
आइये इसे उदाहरण से समझने का प्रयास करते है।
#include <iostream>
|
ऊपर दिया गया program निचे दिया गया output generate करता है।
Reading From a File
Files में से input read करने के लिए आप ifstream class के object को use करते है। इस object को overloaded right shift operator (>>) के साथ यूज़ किया जाता है। इसका general syntax नीचे दिया जा रहा है।इसे नीचे उदाहरण के माध्यम से समझाया जा रहा है। इस उदाहरण में MyFile से वापस name को read किया जा रहा है। इससे पहले ऊपर दिए गए उदाहरण में MyFile में name को write किया गया था।
ifstream ifobj; char yrname[30]; // Opening file ifobj.open("MyFile"); cout<<"Reading data from file..."; // Reading data ifobj>>yrname; // Printing data to screen cout<<yrname; ifobj.close() |
ऊपर दिया गया program निचे दिया गया output generate करता है।
Closing a File
एक बार जब आप file के साथ read/write operation perform कर ले तो आप उस stream को close करते है। इसके लिए आप close() method यूज़ करेंगे। इस method को आप stream object पर call करते है। इसका general syntax नीचे दिया जा रहा है।
C++ File Pointers
जब भी आप C++ के द्वारा किसी file को read और write करने के लिए open करते है तो उसके साथ 2 तरह के pointers associated रहते है। ये pointers file में start से end तक कँही भी move कर सकते है।
- Get pointer - इसे input pointer भी कहा जाता है। ये pointer file में किसी particular location से input read करने के लिए यूज़ किया जाता है। जब भी आप file को read only mode में open करते है तो by default get pointer file के beginning में होता है।
- 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 किया जाता है।
- seekg() - ये function get pointer को argument के रूप में specify की गयी location पर move करता है। Argument में आप byte number pass करते है। इसे execute होने के बाद उसी byte location से input read किया जा सकता है।
- seekp() - ये function put pointer को specify की गयी location पर move करता है। इस function में भी argument के रूप में byte number pass किया जाता है। इसे execute होने के बाद आप उसी location से output store करना प्रारम्भ कर सकते है।
- tellg() - ये function get pointer की current position return करता है।
- tellp() - ये function put pointer की current location return करता है।
Example
#include <iostream>
cout<<"Current position of input file pointer : "<<ifobj.tellg()<<endl;
cout<<"Going to position 10th "<<endl; |
ऊपर दिया गया 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 |
0 Comments