File Handling in C++
File Handling in C++
ks”kk; ue%
~ 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.
ios
istream ostream
iostream <fstream>
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<<“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.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.