





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This is lab manual in form of lecture slides for Introduction to Programming course. It was delivered by Prof. Abhimoda Arora at Alagappa University. It includes: File, Handling, Function, Stream, Class, Read, Write, Derived, Object, Opening, Program
Typology: Exercises
1 / 29
This page cannot be seen from the preview
Don't miss anything!






















#include
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. Let's see an example:
๎ This code creates a file called example.txt and inserts a sentence into it in the same way we are used to do with cout, but using the file stream myfile instead.
๎ The first operation generally performed on an object of one of these classes is to associate it to a real file. This procedure is known as to open a file. An open file is represented within a program by a stream object (an instantiation of one of these classes, in the previous example this was myfile) and any input or output operation performed on this stream object will be applied to the physical file associated to it.
๎ All these flags can be combined using the bitwise operator OR (|). For example, if we want to open the file example.txt in binary mode to add data we could do it by the following call to member function open():
class default mode parameter ofstream ios::out ifstream ios::in fstream ios::in | ios::out
๎ For ifstream and ofstream classes, ios::in and ios::out are automatically and respectively assumed, even if a mode that does not include them is passed as second argument to the open() member function. ๎ The default value is only applied if the function is called without specifying any value for the mode parameter. If the function is called with any value in that parameter the default mode is overridden, not combined.
๎ To check if a file stream was successful opening a file, you can do it by calling to member is_open() with no arguments. 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: ๎ if (myfile.is_open()) { /* ok, proceed with output */ }
๎ 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();
#include
#include
๎ 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.