File Handling-Introduction to Programming-Lab Mannual, Exercises of Computer Programming

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

2011/2012

Uploaded on 07/31/2012

dhairya
dhairya ๐Ÿ‡ฎ๐Ÿ‡ณ

5

(4)

32 documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
File Handling
#include<fstream> //file containing file handling functions
File Handling Functions
> 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
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download File Handling-Introduction to Programming-Lab Mannual and more Exercises Computer Programming in PDF only on Docsity!

File Handling

#include //file containing file handling functions File Handling Functions

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

File Handling

๎€Š 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:

File Handling

๎€Š 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.

Opening a file

๎€Š 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 */ }

Closing a file

๎€Š 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 using namespace std; int main () { ofstream myfile ("example.txt"); if (myfile.is_open()) { myfile << "This is a line.\n"; myfile << "This is another line.\n"; myfile.close(); } else cout << "Unable to open file"; return 0; }

#include #include #include using namespace std; int main () { string line; ifstream myfile ("example.txt"); if (myfile.is_open()) { while ( myfile.good() ) { getline (myfile,line); cout << line << endl; } myfile.close(); } else cout << "Unable to open file"; return 0; }

Checking state flags

๎€Š 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.