Files
Files
Streams-flow of data
Text file
Binary file
SATHIYA.R.R
Stream Class Hierarchy
SATHIYA.R.R
Opening and closing of file-Sample
program
#include<iostream> #include<iostream>
#include<fstream> #include<fstream>
using namespace std; using namespace std;
main() main()
{ or {
ifstream i; ifstream i("in",ios::in);
i.open("in",ios::in); i.close();
i.close(); }
}
Note :
This opens a file named “in” only in input mode. If ifstream is replaced with
ofstream then the file will become only output file. fstream is for both input
and output.
SATHIYA.R.R
File open modes
Sno Open mode Effect File Exists File Does
not exist
4 app Opens the file writing only at the end Not Created
overwritten
SATHIYA.R.R
Alternate Code segment-noreplace
fstream myfile("thefile.txt", ios::in);
if (myfile)
{ // error, file exists! }
else {
myfile.close();
myfile.open("thefile.txt", ios::out);
}
SATHIYA.R.R
Handling File Read failure
#include<iostream> #include<iostream>
#include<fstream> #include<fstream>
using namespace std;
using namespace std;
main() main()
{ {
ifstream i ("in",ios::in);
ifstream i ("in",ios::in); if(!i.is_open())
if(!i)
cout<<“error”;
cout<<“error”; i.close();
}
i.close(); Note: is.open() returns true when
} Open is success.
i.fail() method-returns 1 when open
Note: If open() fails, stream evaluates
fails,0otherwise
to false. SATHIYA.R.R
Text file- Write and Read
#include<iostream> #include<iostream>
#include<fstream> #include<fstream>
using namespace std;
using namespace std;
main()
main() {
{ char name1[5];
int id1;
char name[10]; ifstream input("samp");
int id; //input>>name1;
ofstream o("samp",ios::out); input.getline(name1,5);
cout<<name1<<endl;
cin>>name; input>>id1;
cin>>id; cout<<id1;
}
o<<name<<endl<<id;
} Note:
//i.getline(name1,10,’a’);
SATHIYA.R.R
Text File Read as all characters
#include<iostream> #include<iostream>
#include<fstream> #include<fstream>
using namespace std; using namespace std;
main() main()
{ {
char name1; char name1;
ifstream input("samp"); ifstream input("samp");
while(input) while(input.eof()==0)
{ {
input.get(name1); input.get(name1);
cout<<name1; cout<<name1;
}} }}
Note: If eof -returns 0 Note: If eof-returns non zero value
Otherwise returns 0.
SATHIYA.R.R
fstream example
#include<iostream>
#include<fstream>
using namespace std;
main()
{
char name1; int id1;
fstream f("samp",ios::out);
f<<"hai"<<endl<<10<<endl;
f.close();
f.open("samp",ios::in);
if(f.fail()) cout<<“error”;
else
{while(f.eof()==0)
{f.get(name1);cout<<name1;}}}
SATHIYA.R.R
Binary file –Read and Write
ifstream in("s",ios::in|ios::binary);
#include<iostream>
#include<fstream> in.read((char*)&j,sizeof(int));
using namespace std; in.read((char*)newvalue,5*sizeof(i
main() nt));
{ cout<<j<<endl;
int value[5]={2,4,6,8},newvalue[5];
for(i=0;i<5;i++)
int i=10,j;
cout<<newvalue[i];
ofstream out("s",ios::out|ios::binary);
out.write((char *)&i,sizeof(int)); }
out.write((char*)value,5*sizeof(int));
out.close();
SATHIYA.R.R
Binary file –Read and Write with
single object
main()
#include<iostream>
#include<fstream> {
using namespace std; person p,p1;
class person p.get();
{int id; fstream f("work",ios::out);
char name[10];
f.write((char*)&p,sizeof(person));
public:
f.close();
void get()
{cin>>id>>name;} f.open("work",ios::in);
SATHIYA.R.R
Random File Access
Functions associated with file pointers are:
seekg
tellg
seekp
tellp
seekg/seekp:
Use: Moves get/put file pointers to specified locations based on offset
and origin.
Syntax: object.seekg(offset,origin);/object.seekp(offset,origin);
Offset-position(+ve/-ve integer value)
Origin- ios::beg (default)
ios::cur
ios::end
tellg/tellp syntax:
Use: Tells the current position of get/put file pointers.
Syntax:cout<<object.tellg();/cout<<object.tellp();
SATHIYA.R.R
Random File Access functions(tellp
&tellg)-example
#include<iostream>
#include<iostream>
#include<fstream> #include<fstream>
using namespace std; using namespace std;
#include<bits/stdc++.h> main()
main() {char name1[5];int id1;
{char name[10];
ifstream input("samp");
int id;
cout<<input.tellg();//0
ofstream o("samp",ios::out);
cout<<o.tellp();//0 input>>name1;
#include<fstream>
main(int argc,char*argv[])
ofstream o(argv[1],ios::out);
cin>>name;
cin>>id;
o<<name<<endl<<id;
SATHIYA.R.R
Error handling in Input/output
Error state bits:
e-eof bit (set to 1 if eof ,otherwise set to 0)
usage:object.eof()
f-fail bit(i/o (read /write) operation failed –set to 1)
usage:object.fail()
b-bad bit(Invalid operation/unrecoverable error –set to 1)
usage:object.bad()
g-good bit(set to 1 ,if everything is ok(no flags set) other
usage:object.good() wise set to 0)
SATHIYA.R.R
Error handling in Input/output-
example
#include<iostream>
#include<fstream>
using namespace std;
main()
{
fstream f("work",ios::in);
cout<<f.fail()<<endl<<f.good();
}
If work file existing-0 1
If work file not existing-1 0
SATHIYA.R.R