Using examples explain how files are opened and closed in C++ . Stare any four file modes.
The classes in C++ for file stream operation are :
The file can be opened with two ways:
1. Using the constructor function of the class and
2. Using the member function open() of the class.
Ofstream outfile(“sample.txt”); // output only
Ifstream infile(“sample.txt”); // input only
Using open( ):
Ofstream outfile; // create stream
Outfile.open(“Sample.txt”); // connect stream to sample.txt
The files can be closed using close() function.
Outfile.close( ); // disconnect stream from file buffer
App : append to the end-of-file
Ate : Go to end-of-file on opening
Binary : Binary file
In : Open file for reading only.
Out : open file for write only