File Handling in C++

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 13

File Handling in C++

ios
(facilities to io classes)

istream stream buf ostream


declares provides interface to declares put() and
get(),getline() and physical devices write()
read()

iostream
(contanis all I o
functions )

ifstream fstream ofstream


filebuf

fstreambase
Opening and closing a file
• Syntax
ofstream outfile(“filename”); //output
outfile is an object . Opens a file(filename)
attaches to ostream.

istream infile(“filename”); //input


infile is an object . Opens a file(filename)
attaches to istream.
#include<iostream>
#include<fstream>
using namespace std;
void main()
{
char name[20],address[40];
ofstream out("Test.txt");
cout<<"enter name";
cin>>name;
out<<name<<'\n'; // to file
cout<<"enter address";
cin>>address;
out<<address<<'\n'; // to file
out.close();

ifstream in("Test.txt");
in>>name; //from file
in>>address; //from file
cout<<"Name :"<<name;
cout<<"Address :"<<address;
in.close();
}
Opening and closing a file
• Syntax
ofstream outfile;
outfile.open(“filename”); //output
outfile is an object . Opens a file(filename) attaches to
ostream.
istream infile;
infile.close(); //input
infile is an object . Opens a file(filename) attaches to
istream.
#include<iostream>
#include<fstream>
using namespace std;

Void main()
{
Char name[20],address[40];
Ofstream out;
Out.open(“Test.txt”);
Cout<<“enter name”;
Cin>> name;
Out<<name; // to file
Cout<<“enter address”
Cin>>address;
Out<<address; // to file
Out.close();

Ifstream in;
In.open(“Test.txt”);
In>>name; //from file
In>>address; //from file
Cout<<“Name :”<<name;
Cout<<“Address :”<<address;
In.close();
}
Reading file
#include<iostream>
#include<fstream>
using namespace std;
void main()
{
char c ;//array[100];
ifstream in;
in.open("file2.cpp");
while(in)
{
c=in.get(); // in.getline(array,100);
cout<<c; // cout<<array;
}

in.close();
}
#include<iostream>
#include<fstream>
using namespace std;
void main()
{
char c ;
ifstream in;
in.open("file3.cpp");
for(int i=0;i<300;i++)
{
if(in.eof()!=0) // returns non zero if eof condition in encountered
{}
else
{
c=in.get();
cout<<c;
}
}
in.close();
}
File opening modes
Syntax : stream-object.open9”file name”,mode);
Modes are:
ios::app – append to end of file
ios::ate – go to end of file on opening
ios::binary – binary file
ios:: in – open file only to read
ios::nocreate – open fails if file is not exists
ios::noreplace – open fails if file already exists
ios::out – open file for writing
ios::trunc – delete contents of file
File opening modes
Example:
ofstream myfile;
myfile.open ("example.bin", ios::out | ios::app |
ios::binary);
File pointer and manipulaters
• seekg() – moves input pointer to spec location
• seekp() – moves output pointer to spec
location
• tellg() – gives current pos of i/p pointer
• tellp() – gives current pos of o/p pointer
// obtaining file size
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
long begin,end;
ifstream myfile ("file4.cpp");
begin = myfile.tellg();
myfile.seekg (0, ios::end);
end = myfile.tellg();
myfile.close();
cout<<“starting offset"<<begin;
cout<<“Ending offset"<<end;
cout << “size is: " << (end-begin) << " bytes.\n";
return 0;
}
void main()
#include <iostream>
{ inventory a[3];
#include<fstream>
fstream f;
#include<iomanip>
f.open("stock.txt",ios::in|ios::out);
using namespace std;
cout<<"enter details";
class inventory
for(int i=0;i<3;i++)
{
{
char name[10]; int code; float cost;
public:
a[i].readdata();
void readdata() f.write((char *)&a[i],sizeof(a[i]));
{ cout<<"entername";cin>>name; }
cout<<"enter code";cin>>code; f.seekg(0);
cout<<"enter cost";cin>>cost; } cout<<"output";
void writedata() for(int i=0;i<3;i++)
{ cout<<setiosflags(ios::left)<<setw(10) {
<<name f.read((char*)&a[i],sizeof(a[i]));
<<setiosflags(ios::left)<<setw(10) cout<<"\n";
<<code a[i].writedata();
<<setprecision(3)<<setw(10) }
<<cost;} f.close();
}; }

You might also like