0% found this document useful (0 votes)
51 views

C++010 (Working With Files)

1. The document discusses file handling in C++ programs. It explains that files allow storing data externally instead of just in memory. 2. It describes three main classes - ifstream for input, ofstream for output, and fstream for both - that are used to read and write files through file streams. 3. Opening and closing files involves either passing the file name to the class constructor or using the open() member function. Multiple files can be accessed using a single stream object by closing the file before opening another.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

C++010 (Working With Files)

1. The document discusses file handling in C++ programs. It explains that files allow storing data externally instead of just in memory. 2. It describes three main classes - ifstream for input, ofstream for output, and fstream for both - that are used to read and write files through file streams. 3. Opening and closing files involves either passing the file name to the class constructor or using the open() member function. Multiple files can be accessed using a single stream object by closing the file before opening another.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIT-10 Working with files

Write Data ( to file )

External memory DATA files

Read Data (From file)

Program- File Interaction

Internal memory / Program cin>> ( Get data from Keyboard) cout<< (Put data to Screen )

Console Program Ineraction

Console unit (Screen + Keyboard)

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

UNIT-10 Working with files


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: 9887036260, 9351670259

UNIT-10 Working with files


Stream classes for file operations
ios

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

UNIT-10 Working with files

Created By: Deepak Kumar Prajapati B.E. (Information technology) Mob: 9887036260, 9351670259

UNIT-10 Working with files Opening and closing a file


When we want to use a file, we have to decide the following things about the file: 1. Suitable name of the file: Input.data, Test.doc. 2. Data type and structure 3. Purpose 4. Opening method. We can open a file in two ways: 1. Using the constructor function of the class. This method is useful when we use only one file in the stream.

2. Using the member function open( ) of the class.


This method is used when we want to manage multiple files using one stream. 1. Using Constructor: As we know constructor is used to initialize object while it is being created. Here we use file name to initialize file stream object. Two step method: 1) Create a file stream object to manage the stream while it is being created. 2) Initialize the file object with the desired filename. Example: ofstream outfile(Result); Here we are creating an output stream with object name outfile. This stream object is initialized with File name Result. ifstream infile(Data); Here we are creating an input stream with object name infile. This stream object is initialized with File name Data.

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

UNIT-10 Working with files

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

//Write data to screen //Closing input stream

2. Opening file using open( )


The function open( ) is used to open multiple files that uses the same stream object. Syntax: file-stream-class stream-object; stream-Object.open(student); Example: ofstream outfile; outfile.open(student);
Created By: Deepak Kumar Prajapati B.E. (Information technology) Mob: 9887036260, 9351670259

UNIT-10 Working with files

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

//Creating output stream //Connect stream to file Student

//Disconnect output stream from file Student //Creating output stream //Connect stream to file Result

//Disconnect output stream from file Result

UNIT-10 Working with files


cout<<line; } fin.close(); getch(); }

2. We can also open two different file in different streams

Created By: Deepak Kumar Prajapati B.E. (Information technology) Mob: 9887036260, 9351670259

UNIT-10 Working with files Reading and writing a class object

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

UNIT-10 Working with files


} }

10

Created By: Deepak Kumar Prajapati B.E. (Information technology) Mob: 9887036260, 9351670259

You might also like