C++010 (Working With Files) A
C++010 (Working With Files) A
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: 9672974917
Input stream: It supplies data to the program. It extracts data from file. Output stream: It receives data from the program. It inserts (write) data to the file.
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: 9672974917
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: 9672974917
The connection with the file closed automatically when the stream object
expires (when the program terminates). 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.
Created By: Deepak Kumar Prajapati B.E. (Information technology) Mob: 9672974917
UNIT-10 Working with files 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
Created By: Deepak Kumar Prajapati B.E. (Information technology) Mob: 9672974917
//Disconnect output stream from file Student //Creating output stream //Connect stream to file Result
Disk
Student
Result Connect one file to fin Fig: Stream working on multiple files
fin
Program
Created By: Deepak Kumar Prajapati B.E. (Information technology) Mob: 9672974917
1. ios::out and ios::trunc both are similar. These modes are used to open a
file for writing only. In this mode if the file with given file-name does not exist then a new file is creates. But if the file is already exist all the contents of this file are removed. 2. ios::noreplace If file is already created then no replacement of new file with old file. 3. ios::binary opens file in binary mode only. 4. ios::in Opens file for reading only if file does not exists then new file is created with given file name. 5. ios::nocreate Same as ios::in but if the given file does not exists then it does not create new file 6. ios::app and ios::ate both functions works same. Their function is to open file for appending / modification. But here is only one difference that ios::app mode permits us to add data to the end of file only. ios::ate mode permits us to add data or to modify the existing data anywhere in the file. 7. We can combine two or more mode parameter using the bitwise OR operator.
Created By: Deepak Kumar Prajapati B.E. (Information technology) Mob: 9672974917
moves get pointer (input) to a specified location. moves put pointer (output) to a specified location. gives the current position of the get pointer. gives the current position of the put pointer.
Created By: Deepak Kumar Prajapati B.E. (Information technology) Mob: 9672974917
syntax: seekg(offset, refposition); seekp(offset, refposition); offset represent the number of bytes the file pointer to be moved to the from the location specified by the parameter refposition. offset(required) refposition(Optional) value may be (-m0+m)
ios::beg start of the file ios::cur current position of the pointer ios::end End of file If you do not this parameter then C++ uses the default parameter according to file open mode. Example: ifstream infile; infile.seekg(10); It moves the file pointer to the byte number 10, But in a file byte number starts from 0. So when we moves file pointer to 10th byte that means we are moving file pointer to the 11th byte of file. seek call fout.seekg(0,ios::beg) fout.seekg(0,ios::cur) fout.seekg(0,ios::end) fout.seekg(m,ios::beg) fout.seekg(m,ios::cur) fout.seekg(m,ios::end) fout.seekg(-m,ios::beg) fout.seekg(-m,ios::cur) fout.seekg(-m,ios::end) Action Go to start. Start at the current position. Go to the end of file. Moves to (m+1)th byte in the file Moves forward by m byte from the current position Not possible Not possible Moves backward by m byte from the current position. Moves backward by m byte from the end.
10
OR
fin.get(c);
Created By: Deepak Kumar Prajapati B.E. (Information technology) Mob: 9672974917
11
12
This location gives us the byte number of the first byte of the mth object. Now we can set the file pointer to reach this byte with the help of seekg( ) or seekp( ). Total no. of objects in a file int n = file_size / object_length; file_size = Using tellg( ) or tellp( ) when the file pointer is located at the end of the file. Example:
Created By: Deepak Kumar Prajapati B.E. (Information technology) Mob: 9672974917
13