WORKING WITH FILES
Many real-life problems handle large volumes of data and, in such situations, the console I/O is laborious task as it is volatile and hence repetitive for every run.
Therefore it is better to use some non-volatile devices such as floppy disk or hard disk to store the data. The data is stored in these devices using the concept of files.
A file is a collection of related data stored in a particular area on the disk. Programs can be designed to perform the read and write operations on these files. A program typically involves either or both of the following kinds of data communication: 1. Data transfer between the console unit and the program. 2. Data transfer between the program and a disk file.
9/3/2012
Dept.ofCSE
Console-Program-File Interaction
9/3/2012
Dept.ofCSE
File streams?
Interface between the programs and the files.
1.
Input stream: The stream that supplies data to the program. It extracts (or reads) data from the file.
2.
Output stream: The stream that receives data from the program. It inserts (or writes) data to the file.
9/3/2012
Dept.ofCSE
Input operation involves the creation of an input stream and linking it with the program and the input file. Output operation involves establishing an output stream with the necessary links with the program and the output file.
9/3/2012 Dept.ofCSE 4
CLASSES FOR FILE STREAM OPERATIONS
File handling methods are defined in a set of classes say ifstream, ofstream and fstream. File opening modes are defined in the class ios These classes are declared in fstream.h and therefore it must be included in any program that uses files.
9/3/2012
Dept.ofCSE
Stream classes for file operations (contained in fstream.h)
9/3/2012
Dept.ofCSE
9/3/2012
Dept.ofCSE
OPENING AND CLOSING A FILE
9/3/2012
Dept.ofCSE
DETECTING END-OF-FILE [EOF]
Why? For preventing any further attempt to read data from the file. How? 2 approaches:
1.Using fin object of ifstream class: fin returns a value of 0 if any error occurs in the file operation including the EOF condition. E.g. while(fin) Thus, the while loop terminates when fin returns a value of 0 on reaching the EOF condition. 2.Using eof( ) member function of ios class: eof( ) returns a non-zero value if the EOF condition is encountered, and a 0 , otherwise. E.g. If(fin1.eof( ) != 0) {exit(1);} Thus, the above statement terminates the program on reaching the EOF.
9/3/2012
Dept.ofCSE
General Format of Open() with mode specification:
The fstream class does not provide a mode by default as in ifstream or ofstream classes provide and therefore, the mode is explicitly specified when using an object of fstream class as in the format below. stream-object. open (file-name, mode); mode (called file mode parameter) specifies the purpose for which the file is opened. It can take one (or more) of the following constants defined in the class ios.
9/3/2012
Dept.ofCSE
10
The mode can combine two or more parameters using the bitwise OR operator (symbol | ) as shown below:
file.open("TEXT", ios::in | ios :: out);
where file is an object of fstream class.
9/3/2012
Dept.ofCSE
11
9/3/2012
Dept.ofCSE
12
FILE POINTERS AND THEIR MANIPULATIONS
Provided to move through the files while reading or writing. 2 pointers are:
1. 2.
Input [get-g] pointer is used for reading the contents of a given file location. Output [put-p] pointer is used for writing to a given file location.
Each time an input or output operation takes place, the appropriate pointer is automatically advanced.
9/3/2012
Dept.ofCSE
13
Default Actions on file pointers in different modes:
When a file is opened in read-only mode, the input pointer is automatically set at the beginning so that file can be read from the start. When a file is opened in write-only mode, the existing contents are deleted and the output pointer is set at the beginning. Thus the file is written from the start. In case, an existing file is opened to add more data, the file is opened in 'append' mode. This moves the output pointer to the end of the file (i.e. the end of the existing contents).
9/3/2012
Dept.ofCSE
14
Functions for Manipulation of File Pointers
9/3/2012
Dept.ofCSE
15
Single character Sequential I/O at a time: get( ) and put( )
#include <fstream.h> #include <string.h> main( ) { char string[80]; cout "Enter a string \n"; cin string; int len = strlen(string); fstream file; // input and output stream file.open("TEXT", ios :: in | ios :: out);
9/3/2012
Dept.ofCSE
16
for( int i = 0; i < len; i++ ) file.put(string[i]);
file. seekg (0); char ch; while(file) { file.get(ch) ; cout ch; } }
//put a character to file // go to the start
// get a character from file // display it on screen
9/3/2012
Dept.ofCSE
17
clear( )
At the end of reading the current contents of the file, the program sets the EOF flag on. This prevents any further reading from or writing to the file. The EOF flag is turned off by using the function clear( ), which allows access to the file once again.
9/3/2012
Dept.ofCSE
18
Program to copy contents of one file to the other.
#include<iostream.h> #include<conio.h> #include<fstream.h> void main( ) { clrscr( ); char ch; ifstream fin; fin.open("src.txt"); ofstream fout; fout.open("dest.txt");
// input stream // source file opened for read-only // output stream // destination file opened for write-only
9/3/2012
Dept.ofCSE
19
Program example contd..
while(fin.eof( ) ==0) { fin.get(ch); fout.put(ch); } fin.close( ); fout.close( ); fin.open("dest.txt"); while(fin.eof( ) ==0) // To read from dest.txt file & disp on console { fin.get(ch); cout<<ch; } fin.open("dest.txt"); fin.close( ); getch(); }
9/3/2012 Dept.ofCSE 20