180 likes | 361 Views
DATA FILE HANDLING IN C++. CHAPTER 7. INTRODUCTION. Role of I/O Library : In c++, a file , at its lowest level is interpreted simply as a sequence or streams of bytes. At this level the notion of a data type is absent and I/O Library manages the transfer of these byes .
E N D
DATA FILE HANDLING IN C++ CHAPTER 7
INTRODUCTION Role of I/O Library : • In c++, a file , at its lowest level is interpreted simply as a sequence or streams of bytes. At this level the notion of a data type is absent and I/O Library manages the transfer of these byes. 2) At user level , file consists of seq. of possibly intermixed data types. The I/O Library manages the interface between these two levels.
Note that : • Stream is a sequence of bytes. It is a general name given to a flow of data. • The file operations make use of streams as an interface between the program and the files.
C++ provides the following classes to perform output and input of characters to/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 or indirectly from the classes istream, and ostream. We have already used objects whose types were these classes: cin is an object of class istream and cout is an object of class ostream. Therfore, we have already been using classes that are related to our file streams. And in fact, we can use our file streams the same way we are already used to use cin and cout, with the only difference that we have to associate these streams with physical files
To perform file I/O, you must include the header <fstream> in your program. It defines several classes, including ifstream, ofstream, and fstream. • These classes are derived from istream, ostream, and iostream, respectively. • Remember, istream, ostream, and iostream are derived from ios, so ifstream, ofstream, and fstream also have access to all operations defined by ios. • Another class used by the file system is filebuf, which provides low-level facilities to manage a file stream. Usually, you don't use filebuf directly, but it is part of the other file classes.
The figure shows the hierarchy of the classes used for input and output operations with console and file, ios stream is the base class for istream and outstream which are in turn base classes for iostream.
TEXT FILES STORES INFORMATION IN ASCII FORMAT EACH LINE OF TEXT IS DELIMITED BY EOL CHARACTER. INTERNAL TRANSLATIONS WHEN EOL IS INCURRED. BY DEFAULT FILES. BINARY FILES STORES INFORAMTION IN BINARY FORMAT. NO EOL. NO TRANSLATION FASTER EASIER FOR A PROGRAM TO R/W. DATA FILES
OPENING FILES • Using constructor function of that class eg: ifstream input_file(“DataFile”); In case of output files a new file is created or the data is washed out of old file. • Using the function open(); In order to open a file with a stream object we use its member function open(): open (filename, mode); For opening more then 1 file simultaneously created diff. streams for each file. If sequential processing then one stream .
THE CONCEPT OF FILE MODES • It defines how file to be used: to read from it, write to, to append it etc. • All these flags can be combined using the bitwise operator OR (|).
EXAMPLE • For example, if we want to open the file example. bin in binary mode to add data we could do it by the following call to member function open(): ofstream myfile; myfile.open ("example.bin", ios::out | ios::app | ios::binary);
CLOSING FILES • When we are finished with our input and output operations on a file we shall close it so that its resources become available again. In order to do that we have to call the stream's member function close(). This member function takes no parameters, and what it does is to flush the associated buffers and close the file: • myfile.close(); • Once this member function is called, the stream object can be used to open another file, and the file is available again to be opened by other processes. • In case that an object is destructed while still associated with an open file, the destructor automatically calls the member function close().