Text Files
Text Files
com/doc/tutorial/files/
constructor that automatically calls the open member function and has the exact same parameters as this member.
Therefore, we could also have declared the previous myfile object and conduct the same opening operation in our
previous example by writing:
Combining object construction and stream opening in a single statement. Both forms to open a file are valid and
equivalent.
To check if a file stream was successful opening a file, you can do it by calling to member is_open. This member
function returns a bool value of true in the case that indeed the stream object is associated with an open file, or false
otherwise:
Closing a file
When we are finished with our input and output operations on a file we shall close it so that the operating system is
notified and its resources become available again. For that, we call the stream's member function close. This member
function takes flushes the associated buffers and closes the file:
myfile.close();
Once this member function is called, the stream object can be re-used to open another file, and the file is available
again to be opened by other processes.
In case that an object is destroyed while still associated with an open file, the destructor automatically calls the
member function close.
Text files
Text file streams are those where the ios::binary flag is not included in their opening mode. These files are designed
to store text and thus all values that are input or output from/to them can suffer some formatting transformations,
which do not necessarily correspond to their literal binary value.
Writing operations on text files are performed in the same way we operated with cout:
Reading from a file can also be performed in the same way that we did with cin:
This last example reads a text file and prints out its content on the screen. We have created a while loop that reads the
file line by line, using getline. The value returned by getline is a reference to the stream object itself, which when
evaluated as a boolean expression (as in this while-loop) is true if the stream is ready for more operations, and false
if either the end of the file has been reached or if some other error occurred.
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.
2 of 4 7/24/19, 12:08 PM