हिंदी माध्यम नोट्स
File Basic in c++ language in hindi , ofstream , ifstream , example with source code program c++
इससे पहले हम discuss कर चुके है की iostream standard library मे उपस्थित cin और cout function का use stream से data को read और write करने के लिए किया जाता है |
इस article मे , file से data को read और write करने के लिए एक अलग header file होता है इस header file को fstream कहते है | इस header file मे निन्म functions होते है :
1. ofstream
इस command का use output file stream को repreasent करने के लिए किया जाता है | और इस command का use file को create करने एक लिए किया जाता है और इस file का mode write mode होता है |
उदाहरन :
इस उदाहरन मे , किसी file मे एक data को write करने के लिए निन्न्म code को consider करेगे :
void writefunction(char* file, int value){
ofstream f;
f.open(file);
if (f.add()){
f<<value;
}
f.close();
}
इस code मे
ofstream f statement से file को create किया जाता है जिसका location को file pointer f मे add किया जाता है |
f.open(file) statement से file को open किया जाता है |
f<<value statement से यूजर द्वारा दिए गये data को file मे write किया जाता है |
2. ifstream
इस command का use input file stream को repreasent करने के लिए किया जाता है | और इस command का use file को read करने एक लिए किया जाता है औरइस command से file को create नहीं किया जा सकता है इसलिए जब file exit नहीं होती है तब इस command से error message occur होता है |
उदाहरन :
इस उदाहरन मे , किसी file मे एक data को read करने के लिए निन्न्म code को consider करेगे :
#include<iostream>
#include<fstream>
using namespace std;int main()
{
ifstream f(“D:\\file1.txt”);
char a[80];
if(!f)
{
cout << “Error is encountered” << endl;
}
else
{
cout << “File is successfully opened” << endl;
}
while(!f.eof())
{
f >> a;
cout << a << endl;
}
getch();
}
इस code मे
ifstream f statement से file को read किया जाता है जिसका location को file pointer f मे add किया जाता है |
f >> a statement से file मे लिखे data को array a मे सेव किया जाता है |
इसके बाद a की value को write किया जाता है |
3. fstream
इस command का मतलब file stream को define करने के लिए किया जाता है इस command मे ofstream and ifstream की capablity को hold करता है |इसका मतलब है इस command से file को create किया जाता है और इसके अलावा create file मे data को read और write किया जाता है |
इन सभी command को c++ प्रोग्राम मे use करने के लिए header files <iostream> and <fstream> को include किया जाता है |
उदाहरन :
इस उदाहरन मे , किसी file से data को read और write भी किया जाता है | इसके लिए fstream class को use किया जाता है | इसके लिए fstream class के object f को define किया जाता है | और पुरे code मे ‘f’ को ही use किया जाता है |
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string line;
vector dict;
ifstream f(“file path “);
if( !f ) {
cout << “Error opening input file” << endl ;
exit(1) ;
}
while(!f.eof()) {
getline(f,line);
if(line[0] >= ‘a’ && line[0] <= ‘z’)
dict.push_back(line);
}
for(int i = 0; i < dict.size(); i++) {
cout << dict[i] << ” ” << endl;
}
}
इस code मे fstream::eof() statement को use किया गया है जो की error को find करता है | इसके लिए कुछ sepicific condition होती है | जिसका code निन्म है :
while(!dict_reader_pointer .eof()) {
getline(dict_reader_pointer ,line);
while(getline(dict_reader_pointer ,line)) {
getline () statement से किसी line को read किया जाता है लेकिन reading operation तब तक चलता है जब तक line में space नहीं आ जाता है |
Ram live in San Francisco ,123 , America
Sita live in Lava Canada,450-978
इस आउटपुट को print करने एक लिए consider कर सकता है सभी line को तब और line फीड से separate किया गया है और इस code को file मे exactly write करने के लिए निन्म code को use किया जता है |
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
const int RECORDS = 12;
ifstream reader_pointer (“PhoneBook.txt”);
if(!reader_pointer ) {
cout << “Error: cannot open input file” << endl;
return -1;
}
string line[RECORDS];
int i = 0;
while(!reader_pointer .eof()) {
if((i+1) % 4 == 0)
getline(reader_pointer ,line[i++],’\n’);
else
getline(reader_pointer ,line[i++],’\t’);
}
i = 0;
while(i < RECORDS) {
cout << “First name ” << line[i++] << endl;
cout << “Last name ” << line[i++] << endl;
cout << “Area ” << line[i++] << endl;
cout << “Phone ” << line[i++] << endl << endl;
}
reader_pointer.close();
}
Recent Posts
मालकाना का युद्ध malkhana ka yudh kab hua tha in hindi
malkhana ka yudh kab hua tha in hindi मालकाना का युद्ध ? मालकाना के युद्ध…
कान्हड़देव तथा अलाउद्दीन खिलजी के संबंधों पर प्रकाश डालिए
राणा रतन सिंह चित्तौड़ ( 1302 ई. - 1303 ) राजस्थान के इतिहास में गुहिलवंशी…
हम्मीर देव चौहान का इतिहास क्या है ? hammir dev chauhan history in hindi explained
hammir dev chauhan history in hindi explained हम्मीर देव चौहान का इतिहास क्या है ?…
तराइन का प्रथम युद्ध कब और किसके बीच हुआ द्वितीय युद्ध Tarain battle in hindi first and second
Tarain battle in hindi first and second तराइन का प्रथम युद्ध कब और किसके बीच…
चौहानों की उत्पत्ति कैसे हुई थी ? chahamana dynasty ki utpatti kahan se hui in hindi
chahamana dynasty ki utpatti kahan se hui in hindi चौहानों की उत्पत्ति कैसे हुई थी…
भारत पर पहला तुर्क आक्रमण किसने किया कब हुआ first turk invaders who attacked india in hindi
first turk invaders who attacked india in hindi भारत पर पहला तुर्क आक्रमण किसने किया…