0% found this document useful (0 votes)
19 views10 pages

File Handling in C++

Uploaded by

bansalakash7896
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views10 pages

File Handling in C++

Uploaded by

bansalakash7896
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Jh x.

ks”kk; ue%

File Handling in C++

~ Anni Kumari
~ Akash bansal
What is File Handling?
RAM File

Write

Variables

Read
Program’s Memory
Secondary
Storage
File handling Implementation
1. Create an object of input or output stream.

2. Open a file for destination or source.

3. Write data in file or Read data from file.

4. After read or write you must close the file.


Predefined Stream Classes
Ios_base

ios

istream ostream

iostream <fstream>

ifstream fstream ofstream


File Opening Modes

1. in Open For Reading

2. out Open For Writing

Syntax
3. app Open to append
ios :: in
ios :: out
4. binary Open file as binary ios :: app
ios :: binary
Write data in a file

Ofstream fout;

fout.open(“File1.txt”, ios :: out);

fout<<“Hello world”;

fout.close( );
Read data in a file
char x;
ifstream fin;
fin.open(“file1.txt”, ios :: in);
if( ! fin)
cout<<“File not found”;
else{
x=fin.get( );
while(!fin.eof() )
{ x= fin.get( ); }
fin.close( );
}
Write data of object in a file
Ofstream fout;

fout.open(“File1.txt”, ios :: app | ios :: binary);

fout.write( (char*) &obj, sizeof(obj) );

fout.close( );
Read data in a object from a file

ifstream fin;
fin.open(“file1.txt”, ios :: in | ios :: binary);
if( ! fin)
cout<<“File not found”;
else{
fin.read( (char*) &obj, sizeof(obj) );
while(!fin.eof())
{fin.read( (char*) &obj,
sizeof(obj)); }
fin.close( );
}
ANY DOUBTS ?

Thank You.

You might also like