JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now

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

Class 6

Hindi social science science maths English

Class 7

Hindi social science science maths English

Class 8

Hindi social science science maths English

Class 9

Hindi social science science Maths English

Class 10

Hindi Social science science Maths English

Class 11

Hindi sociology physics physical education maths english economics geography History

chemistry business studies biology accountancy political science

Class 12

Hindi physics physical education maths english economics

chemistry business studies biology accountancy Political science History sociology

Home science Geography

English medium Notes

Class 6

Hindi social science science maths English

Class 7

Hindi social science science maths English

Class 8

Hindi social science science maths English

Class 9

Hindi social science science Maths English

Class 10

Hindi Social science science Maths English

Class 11

Hindi physics physical education maths entrepreneurship english economics

chemistry business studies biology accountancy

Class 12

Hindi physics physical education maths entrepreneurship english economics

chemistry business studies biology accountancy

Categories: c++ language in hindi

File Open & Close Operation in c++ language , opening and closing a file in c++ pdf

opening and closing a file in c++ pdf , File Open & Close Operation in c++ language
इससे पहले के article मे , fstream , ostream और istream को discuss किया था इस article मे file को open , operation और close operation को discuss करगे |Opening a File
किसी file को use करने से पहले open किया जाता है | इसके लिए fstream , ostream को use किया जाता है | इस class के object से file को create भी किया जाता है और इसे open भी किया जा सकता है |  दोनों ही type के object से create file मे write operation को perform किया जा सकता है | इसके अलावा istream ओबेज्क्ट से भी file को open किया जा सकता है लेकिन इसमें file mode read ही होता है |
file को open करने के लिए निन्म syntax होता है :-
open() : ये file को open करना का syntax होता है | इसकी header file fstream होती है | ओए इसके लिए तीनो fstream , ostream और istream object support होता है |  इसके लिए निन्म syntax होता है :
void open(const char *filename, ios::openmode mode);
इसमें
const char *filename: ये file pointer होता है जिसमे file की location को add किया जाता है |
ios::openmode mode : ये file को mode को define करता है | इसके लिए statndrad flag को define किया गया है : जिसकी table निन्म है :

1.ios::app
इस mode मे file को write operation होता है लेकिन अगर file मे पहले से कुछ लिखा होता है तो इस mode मे file को लिखे गये content के बाद से write operation को perform किया जाता है | इसके append mode भी कहते है | जो की file मे apapending operation परफोर्म करता है |

2.ios::ate
इस mode से use किसी file को आउटपुट operation के लिए open करने के लिए किया जाता है | इस mode मे file मे read और write दोनों operation को perform करता है जो की file के end तक चलता है |
3.ios::in
इस mode मे file को reading mode मे open किया जाता है | इसमें file मे उपस्थित content को read किया जा सकता है |
4.ios::out
इस mode मे file को writing mode मे open किया जाता है | इसमें file मे  content को write किया जा सकता है |
5.ios::trunc
इस mode मे file को open किया जाता है लेकिन अगर file पहले से उपस्थित है तब इस file मे उपस्थित content को डिलीट कर दिया जाता है बाद मे file को open किया जाता है |

close a file
c++ language मे , जब किसी file को open किया जाता है तब इस file को क्लोज भी जरुर किया जाता है इसके लिए निन्म syntax को use किया जता है :-

void close();

जब एक या एक से ज्यादा file को code मे use किया जाता है तब multiple close statement को use किया जाता है |
और void की जगह पर file pointer को use किया जाता है उदाहरन के लिए अगर file pointer f नही तब इसका close statement निन्म होगा :
f.close();
और अगर file pointer ‘file’ है तब इसका क्लोज statement निन्म होगा
file.close();

Writing to a File
जब c++ programming मे किसी file कमी content को लिखा जाता है तब इस operation के लिए stream insertion operator (<<) को use किया जाता है  | इस code का use केवल content को file से stream मे display करने के लिए किया जाता है | और इस code मे पहले वाले code मे केवल एक difference है cout के जगह पर  ofstream or fstream object को use किया जाता है |

Reading from a File
जब c++ programming मे किसी file कमी content को पढ़ा जाता है तब इस operation के लिए stream insertion operator (>>) को use किया जाता है  | इस code का use केवल content को file से content को पढने के लिए किया जाता है | और इस code मे पहले वाले code मे केवल एक difference है cout के जगह पर  ofstream or fstream object को use किया जाता है |

Read and Write Example
इस उदहारण मे किसी file से content को लिखा जाता है और इस content को बाद मे पढ़ा  भी जाता है | जब किसी file मे से content को लिखा जाता है उस file का नाम content.txt है | और बाद मे इस file से content को read करके screen मे print किया जाता है |

code
#include <fstream>
#include <iostream>
using namespace std;
void  main () {
char content[100];
// open a file in write mode.
ofstream outfile;
outfile.open(“data.text”);
cout << “Writing to the file” << endl;
cout << “Enter your name: “;
cin.getline(content, 100);
// write inputted content into the file.
outfile << content << endl;
cout << “Enter your age: “;
cin >> content;
cin.ignore();

// again write inputted content into the file.
outfile << content << endl;
// close the opened file.
outfile.close();
// open a file in read mode.
ifstream infile;
infile.open(“data.text”);

cout << “Reading from the file” << endl;
infile >> content;
// write the content at the screen.
cout << content << endl;

// again read the content from the file and display it.
infile >> content;
cout << content << endl;
// close the opened file.
infile.close();
return 0;
}
जब इस code को execute किया जाता है तब इसका आउटपुट निन्म होगा :-
Writing to the file
Enter your name: Parth
Enter your age: 21
Reading from the file
Parth
21

Sbistudy

Recent Posts

four potential in hindi 4-potential electrodynamics चतुर्विम विभव किसे कहते हैं

चतुर्विम विभव (Four-Potential) हम जानते हैं कि एक निर्देश तंत्र में विद्युत क्षेत्र इसके सापेक्ष…

2 days ago

Relativistic Electrodynamics in hindi आपेक्षिकीय विद्युतगतिकी नोट्स क्या है परिभाषा

आपेक्षिकीय विद्युतगतिकी नोट्स क्या है परिभाषा Relativistic Electrodynamics in hindi ? अध्याय : आपेक्षिकीय विद्युतगतिकी…

4 days ago

pair production in hindi formula definition युग्म उत्पादन किसे कहते हैं परिभाषा सूत्र क्या है लिखिए

युग्म उत्पादन किसे कहते हैं परिभाषा सूत्र क्या है लिखिए pair production in hindi formula…

6 days ago

THRESHOLD REACTION ENERGY in hindi देहली अभिक्रिया ऊर्जा किसे कहते हैं सूत्र क्या है परिभाषा

देहली अभिक्रिया ऊर्जा किसे कहते हैं सूत्र क्या है परिभाषा THRESHOLD REACTION ENERGY in hindi…

6 days ago

elastic collision of two particles in hindi definition formula दो कणों की अप्रत्यास्थ टक्कर क्या है

दो कणों की अप्रत्यास्थ टक्कर क्या है elastic collision of two particles in hindi definition…

6 days 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