|
C++ Programming Tutorials
Text files
Text file streams are those where we do not include the ios::binary flag
in their opening mode. These files are designed to store text and thus
all values that we input or output from/to them can suffer some
formatting transformations, which do not necessarily correspond to their
literal binary value.
Data output operations on text files are performed in the same way we
operated with cout:
// writing on a text file
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile ("example.txt");
if (myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";
return 0;
} |
[file example.txt]
This is a line.
This is another line.
|
Data input from a file can also be performed in the same way
that we did with cin:
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
} |
This is a line.
This is another line.
|
This last example reads a text file and prints out its
content on the screen. Notice how we have used a new member function, called
eof() that returns true in the case that the end of the file has been reached.
We have created a while loop that finishes when indeed myfile.eof() becomes true
(i.e., the end of the file has been reached).
Checking state flags
In addition to eof(), which checks if the end of file has been reached, other
member functions exist to check the state of a stream (all of them return a bool
value):
bad()
Returns true if a reading or writing operation fails. For example in the case
that we try to write to a file that is not open for writing or if the device
where we try to write has no space left.
fail()
Returns true in the same cases as bad(), but also in the case that a format
error happens, like when an alphabetical character is extracted when we are
trying to read an integer number.
eof()
Returns true if a file open for reading has reached the end.
good()
It is the most generic state flag: it returns false in the same cases in which
calling any of the previous functions would return true.
In order to reset the state flags checked by any of these member functions we
have just seen we can use the member function clear(), which takes no
parameters.
get and put stream pointers
All i/o streams objects have, at least, one internal stream pointer:
ifstream, like istream, has a pointer known as the get pointer that points to
the element to be read in the next input operation.
ofstream, like ostream, has a pointer known as the put pointer that points to
the location where the next element has to be written.
Finally, fstream, inherits both, the get and the put pointers, from iostream
(which is itself derived from both istream and ostream).
These internal stream pointers that point to the reading or writing locations
within a stream can be manipulated using the following member functions:
tellg() and tellp()
These two member functions have no parameters and return a value of the member
type pos_type, which is an integer data type representing the current position
of the get stream pointer (in the case of tellg) or the put stream pointer (in
the case of tellp).
NEXT >>
seekg() and seekp()
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
C Aptitude Questions
for more C Aptitude Interview Questions with Answers
Check
C Interview Questions
for more C Interview Questions with Answers.
|