1 / 16

DATA FILE HANDLING IN C++

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 .

kimball
Download Presentation

DATA FILE HANDLING IN C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DATA FILE HANDLING IN C++ CHAPTER 7

  2. 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.

  3. 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.

  4. 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

  5. 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.

  6. 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.

  7. EXAMPLE

  8. 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

  9. 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 .

  10. CLASS AND ITS DEFAULT PARAMETERS

  11. 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 (|).

  12. FILE MODES CONSTANTS

  13. 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);

  14. 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().

  15. EXAMPLE

  16. THANK YOU….

More Related