Advertisement Remove all ads

Using Examples Explain How Files Are Opened and Closed in C++ . Stare Any Four File Modes. - Computer Science 1

Advertisement Remove all ads
Advertisement Remove all ads
Advertisement Remove all ads

Using examples explain how files are opened and closed in C++ . Stare any four file modes.

Advertisement Remove all ads

Solution

The classes in C++ for file stream operation are :
a. filebuf
b. fstreambase
c. ifstream
d. ofstream
e. fstream
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.


Using Constructor:
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


File modes:
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

Concept: C++ Programming
  Is there an error in this question or solution?

APPEARS IN

Advertisement Remove all ads
Share
Notifications

View all notifications


      Forgot password?
View in app×