C++010 (Working With Files)
C++010 (Working With Files)
Internal memory / Program cin>> ( Get data from Keyboard) cout<< (Put data to Screen )
A program involves two types of communication: 1. Data transfer between the console unit and the program. 2. Data transfer between the program and disk File. In Normal programming data is stored in main or primary memory. So after the completion of the execution of program, the inputted data will be destroyed. Each time when you executing the program, you have to input data again - and again. This will become a very tedious job if the input data size is very large. Sometimes we need to store our data on secondary storage device like floppy, Hard disk etc. Data storage on such devices uses the concept of files. A file is a collection of related data stored in a particular area on the disk. We can design some programs to perform the read and write operations on these files. The I/O system of C++ handles file operation similar to the console input and output operations. It uses file streams as an interface between the programs and files.
Created By: Deepak Kumar Prajapati B.E. (Information technology) Mob: 9887036260, 9351670259
Read Data
Input Stream
Input data
Disk file
Write data
Program
Output Stream
Data output
Classes for file stream operations The I/O system of C++ uses a set of classes that defines the file handling operation. For file operations it uses classes ifstream, ofstream and fstream. All these classes derived from class fstreambase and corresponding iostream class. We must include file fstream in any program that uses files.
Created By: Deepak Kumar Prajapati B.E. (Information technology) Mob: 9887036260, 9351670259
istream
streambuf
ostream
iostream file
iostream
ifstream
fstream
ofstream
filebuf
fstream file
fstreambase
Class filebuf
Contents It sets the file buffers to read and write. It contains open( ) and close( ) as member. fstreambase Provides operations common to the file stream. Serves as a base for fstream, ifstream, ofstream class. Contains open( ) and close( ) function. ifstream Provides input operations. Contains open with default input mode. Inherits the functions get( ), getline( ), seekg( ) and tellg( ) functions from istream. ofstream Provides output operations. Contains open( ) with default output mode. Inherits the functions put( ), seekp( ), tellp( ) and write( ) from ostream. fstream Provide support for simultaneous input and output operations. Contains open( ) with default input mode. Inherits all the functions from istream and ostream classes through iostream.
Created By: Deepak Kumar Prajapati B.E. (Information technology) Mob: 9887036260, 9351670259
Created By: Deepak Kumar Prajapati B.E. (Information technology) Mob: 9887036260, 9351670259
The connection with the file closed automatically when the stream object
expires (when the program terminates).
Created By: Deepak Kumar Prajapati B.E. (Information technology) Mob: 9887036260, 9351670259
Sometimes we need to open a file for both input and output purpose. So
instead of using two programs for each purpose we a use a single program that can open the same file for both input and output but one by one. That means first open the file for input using input stream. After reading close it and open the same file for output using output stream. Example: Creating file using constructor function
#include<iostream.h> #include<fstream.h> void main() { ofstream outfile(D:\\student.doc); //connect file ITEM to output stream cout<<\nEnter item name: ; char name[30]; cin>>name; cout<<\nEnter roll no. ; int rn; cin>>rn; outfile<<name<<\t<<rn<<\n; outfile.close( ); ifstream infile(D:\\student.doc); infile >> name; infile >> rn; cout<<name<<\t<<rn; infile.close( ); }
//Write data to file //Closing output stream. //Connect file ITEM to input stream //Extract data from file
1. We can open multiple file using single stream object, but one file should be closed before opening other file. Example: ofstream outfile; outfile.open(Student); .. . outfile.close( ); ofstream outfile; outfile.open(Result); .. . outfile.close( ); Program:
#include<iostream.h> #include<fstream.h> #include<conio.h> #include<iomanip.h> void main() { ofstream fout; clrscr(); fout.open("D:\\country.doc"); fout<<"United states of America"; fout<<"India"; fout<<"Austrelia"; fout.close(); fout.open("D:\\Capital.doc"); fout<<"Washington"; fout<<"Delhi"; fout<<"Kenbara"; fout.close(); const int N=80; char line[N]; ifstream fin; fin.open("D:\\country.doc"); cout<<"\nContents of country file\n\n"; while(fin) { fin.getline(line,N); cout<<line; } fin.close(); fin.open("D:\\Capital.doc"); cout<<"\nContents of Capital file\n\n"; while(fin) { fin.getline(line,N);
Created By: Deepak Kumar Prajapati B.E. (Information technology) Mob: 9887036260, 9351670259
//Disconnect output stream from file Student //Creating output stream //Connect stream to file Result
Created By: Deepak Kumar Prajapati B.E. (Information technology) Mob: 9887036260, 9351670259
The I/O system of C language cannot handle user defined data types such as class object. But class and objects are the central element of C++ programming , so C++ has binary input and output function read( ) and write( ) for this purpose. These functions can handle entire structure of an object as a single unit. Function write( ) copies a class object from memory, byte to byte with no conversion. Example:
#include<iostream.h> #include<fstream.h> #include<conio.h> #include<iomanip.h> class student { char name[30]; int rn; char br[10]; public: void readdata(); void writedata(); }; void student::readdata() { cout<<"\nEnter name: "; cin>>name; cout<<"\nRoll no.: "; cin>>rn; cout<<"\nEnter Branch: "; cin>>br; } void student::writedata() { cout<<setiosflags(ios::left)<<setw(10)<<name<<setiosflags(ios::right)<<setw(10)<<rn <<setw(10)<<br<<endl; } void main( ) { student st[3]; fstream file; file.open("D:\\student.doc",ios::in | ios::out); cout<<"\nEnter details for three students\n"; for(int i=0;i<3;i++) { st[i].readdata(); file.write((char *) & st[i],sizeof(st[i])); } file.seekg(0); cout<<"\nOutput\n\n"; for(i=0;i<3;i++) { file.write((char *) & st[i],sizeof(st[i])); st[i].writedata();
Created By: Deepak Kumar Prajapati B.E. (Information technology) Mob: 9887036260, 9351670259
10
Created By: Deepak Kumar Prajapati B.E. (Information technology) Mob: 9887036260, 9351670259