हिंदी माध्यम नोट्स
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
सती रासो किसकी रचना है , sati raso ke rachnakar kaun hai in hindi , सती रासो के लेखक कौन है
सती रासो के लेखक कौन है सती रासो किसकी रचना है , sati raso ke…
मारवाड़ रा परगना री विगत किसकी रचना है , marwar ra pargana ri vigat ke lekhak kaun the
marwar ra pargana ri vigat ke lekhak kaun the मारवाड़ रा परगना री विगत किसकी…
राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए sources of rajasthan history in hindi
sources of rajasthan history in hindi राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए…
गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है ? gurjaratra pradesh in rajasthan in hindi
gurjaratra pradesh in rajasthan in hindi गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है…
Weston Standard Cell in hindi वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन
वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन Weston Standard Cell in…
polity notes pdf in hindi for upsc prelims and mains exam , SSC , RAS political science hindi medium handwritten
get all types and chapters polity notes pdf in hindi for upsc , SSC ,…