Working With Files: A File Is A Collection of Data Stored in A Particular Area On The
Working With Files: A File Is A Collection of Data Stored in A Particular Area On The
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
9/3/2012
Dept.ofCSE
9/3/2012
Dept.ofCSE
9/3/2012
Dept.ofCSE
9/3/2012
Dept.ofCSE
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
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);
9/3/2012
Dept.ofCSE
11
9/3/2012
Dept.ofCSE
12
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
9/3/2012
Dept.ofCSE
14
9/3/2012
Dept.ofCSE
15
9/3/2012
Dept.ofCSE
16
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
#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