Download C++ File Handling: Input/Output with Files using ofstream, ifstream and fstream and more Slides Computer Programming in PDF only on Docsity!
C++ Programming^ โข File handling
Input/Output with files^ ๏^ C++ provides the following classes toperform output and input of charactersto/from files:^ โฆ
ofstream:
Stream class to write on files
โฆ^ ifstream:
Stream class to read from files
โฆ^ fstream:
Stream class to both read and write
from/to files.
๏^ These classes are derived directly orindirectly from the classes istream, andostream.
Open a file(Contd.)^ ๏^ An open file is represented within a program bya stream and any input or output operationperformed on this stream object will be appliedto the physical file associated to it.^ ๏^ In order to open a file with a stream object weuse its member function open():^ โฆ
open (filename, mode); โฆ Where filename is a null-terminated charactersequence of type const char * representing the nameof the file to be opened โฆ mode is an optional parameter with a combination ofthe flags.
Open a file(Contd.)^ ๏^ Flags:
Open a file(Contd.)^ ๏^ Each one of the open() member functionsof the classes ofstream, ifstream andfstream has a default mode that is used ifthe file is opened without a secondargument:
Open a file(Contd.)^ ๏^ To check if a file stream was successfulopening a file, you can do it by calling tomember is_open() with no arguments.^ โฆ
This member function returns a bool value oftrue in the case that indeed the stream objectis associated with an open file, or falseotherwise:
Closing a file(Contd.)^ ๏^ Once this member function is called, the streamobject can be used to open another file, and thefile is available again to be opened by otherprocesses.^ ๏^ In case that an object is destructed while stillassociated with an open file, the destructorautomatically calls the member function close().
Text files^ ๏^ Text file streams are those where we donot include the ios::binary flag in theiropening mode.^ ๏^ These files are designed to store text andthus all values that we input or outputfrom/to them can suffer some formattingtransformations, which do not necessarilycorrespond to their literal binary value.
Text files(Contd.)^ ๏^ Data input from a file can also be performed inthe same way that we did with cin:
Text files(Contd.)^ ๏^ This last example reads a text file and prints outits content on the screen.^ ๏^ Notice how we have used a new memberfunction, called good() that returns true in thecase that the stream is ready for input/outputoperations.^ ๏^ We have created a while loop that finisheswhen indeed myfile.good() is no longer true,which will happen either if the end of the filehas been reached or if some other erroroccurred
.^
Checking state flags (Contd.)^ ๏^ Fail():
Returns true in the same cases as bad(), but also in the case that a format error happens, like when analphabetical character is extracted when we are tryingto 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 previousfunctions would return true.
get and put stream pointers^ ๏^ All i/o streams objects have, at least, oneinternal 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 asthe^
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 derivedfrom both istream and ostream).
get and put stream pointers(Contd.)^ ๏^ tellg() and tellp()^ โฆ
these two member functions have noparameters and return a value of the membertype pos_type, which is an integer data typerepresenting the current position of the getstream pointer (in the case of tellg) or the putstream pointer (in the case of tellp).
get and put stream pointers(Contd.)^ ๏^ seekg() and seekp()^ โฆ
These functions allow us to change theposition of the get and put stream pointers.Both functions are overloaded with twodifferent prototypes.
The first prototype is: seekg ( position );seekp ( position );