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

Files

Uploaded by

sathiya
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Files

Uploaded by

sathiya
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

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

1 in Opens file for reading Error

2 out Opens the file for writing Truncated/ Created


overwritten

3 trunc Opens the file for writing Truncated/ Created


overwritten

4 app Opens the file writing only at the end Not Created
overwritten

5 ate Opens the file writing at the end in Not Created


beginning then it allows to add overwritten
anywhere in the file
6 nocreate Open fails if file already not exists. Error
7 noreplace Open for writing should fail if file Error
already exists.

8 binary Opens file in binary mode

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);

void put() f.read((char *)&p1,sizeof(p1));


{cout<<id<<name;} p1.put();
}; f.close();
}
SATHIYA.R.R
Binary file –Read and Write with
array of objects
main()
#include<iostream>
#include<fstream> {
using namespace std; person p[5],p1[5]; int i;
class person for(i=0;i<5;i++)
{int id; p[i].get();
char name[10];
fstream f("work",ios::out);
public:
f.write((char*)p,5*sizeof(person));
void get()
{cin>>id>>name;} f.close();

void put() f.open("work",ios::in);


{cout<<id<<name; f.read((char *)p1,5*sizeof(p1));
} for(i=0;i<5;i++)
};
p1[i].put();
SATHIYA.R.R
f.close();}
Pointers with Files

Two pointers are associated with each file


 Get pointer
 Put pointer

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;

cin>>name; cin>>id; cout<<name1;//hai


o<<name<<endl; cout<<o.tellp();//4 input>>id1;cout<<input.tellg();//6
o<<id;cout<<o.tellp();//6 cout<<id1;//10
}
}
o/p:0 4 6 SATHIYA.R.R
o/p:0hai610
Random File Access
functions(seekp &seekg)-example
#include<iostream>
#include<iostream>
#include<fstream>
using namespace std; #include<fstream>
#include<bits/stdc++.h> using namespace std;
main() main()
{char name[10]; int id; {char name;
fstream o("samp",ios::out);
ifstream input("samp");
cout<<o.tellp();//0
cout<<input.tellg();//0
cin>>name; cin>>id;
o<<name<<endl<<id; input.seekg(4);
cout<<o.tellp();//6 cout<<input.tellg();//4
o.seekp(1,ios::beg); cout<<o.tellp();//1 input>>name; cout<<name;//h
o<<'o‘; } }
Before: hai After:hoi
o/p:0 4 h(Content in samp file :
10 10
SATHIYA.R.R hai hello welcome)
Random File Access functions in
objects
#include<iostream> f.open("work",ios::in);
#include<fstream>
f.seekg(sizeof(person));
using namespace std;
f.read((char *)&p1,sizeof(p1));
class person
{ int id; char name[10]; p1.put();
public: f.close();}
void get(){ cin>>id>>name;} o/p:
void put() {cout<<id<<name; } }; 10
main()
sri
{ person p[3],p1; int i; 20
for(i=0;i<3;i++) p[i].get(); ram
fstream f("work",ios::out); 30
f.write((char*)p,5*sizeof(person)); dharsa
f.close(); 20ram
SATHIYA.R.R
Command line Arguments
#include<iostream> #include<iostream>
#include<fstream> #include<bits/stdc++.h>
using namespace std; #include<fstream>
main(int argc,char*argv[])
using namespace std;
{ int i;
main(int argc,char*argv[])
cout<<"The count of command line
arguments:"<<argc<<endl; //2 {
for(i=0;i<argc;i++) int i;
cout<<argv[i]<<endl; cout<<"The count of command line
}
arguments:"<<argc<<endl;
o/p:
cout<<atoi(argv[1])+atoi(argv[2]);
$./a.out samp
2 cout<<endl;
./a.out }
o/p:
samp ./a.out 10 20
The count of arguments:3
SATHIYA.R.R
30
Command line Arguments
#include<iostream>

#include<fstream>

using namespace std;

main(int argc,char*argv[])

{ char name[10]; int id;

ofstream o(argv[1],ios::out);

cin>>name;

cin>>id;

o<<name<<endl<<id;

$./a.out samp SATHIYA.R.R


Command line Arguments
#include<iostream> person p,p1;
#include<fstream> p.get();
using namespace std; fstream f(v[1],ios::out);
class person
f.write((char*)&p,sizeof(person));
{int id;
f.close();
char name[10];
f.open(v[1],ios::in);
public:
void get() f.read((char *)&p1,sizeof(p1));
{cin>>id>>name;} p1.put();
void put() f.close();
{cout<<id<<name;} }
};
./a.out work
main(int c,char *v[])
{

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

You might also like