JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now

हिंदी माध्यम नोट्स

Categories: c++ language in hindi

File Basic in c++ language in hindi , ofstream , ifstream , example with source code program c++

ofstream , ifstream , example with source code program c++ , File Basic in c++ language in hindi :-
इससे पहले के article मे हमने scoreboard प्रोजेक्ट मे function मे file से data को access और सेव किया गया था अब इस article मे file handling को discuss करेगे | जिसे c++ language मे use किया जाता है |
इससे पहले हम 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();
}

Sbistudy

Recent Posts

Question Tag Definition in english with examples upsc ssc ias state pcs exames important topic

Question Tag Definition • A question tag is a small question at the end of a…

2 weeks ago

Translation in english grammer in hindi examples Step of Translation (अनुवाद के चरण)

Translation 1. Step of Translation (अनुवाद के चरण) • मूल वाक्य का पता करना और उसकी…

2 weeks ago

Report Writing examples in english grammer How to Write Reports explain Exercise

Report Writing • How to Write Reports • Just as no definite rules can be laid down…

2 weeks ago

Letter writing ,types and their examples in english grammer upsc state pcs class 12 10th

Letter writing • Introduction • Letter writing is an intricate task as it demands meticulous attention, still…

2 weeks ago

विश्व के महाद्वीप की भौगोलिक विशेषताएँ continents of the world and their countries in hindi features

continents of the world and their countries in hindi features विश्व के महाद्वीप की भौगोलिक…

2 weeks ago

भारत के वन्य जीव राष्ट्रीय उद्यान list in hin hindi IAS UPSC

भारत के वन्य जीव भारत में जलवायु की दृष्टि से काफी विविधता पाई जाती है,…

2 weeks ago
All Rights ReservedView Non-AMP Version
X

Headline

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings
JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now