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