UNIT NO-5 File Operation
UNIT NO-5 File Operation
=================================================================================
Lecture No 2.
************File Operation****************
=========================================================
***Stream:
-It is a collection of continuous group of data.
-A Stream is sequence of bytes.
-We use get pointer for reading the contents of the file.
-Syntax:
Seekg(position);
2)Seekp()Function:
-It is used to set the position of put pointer.
-We use put pointer for writing files.
-Syntax:
Seekp(position);
3)tellg(position):
-It is used to get current position of get pointer.
-Syntax:
int tellg();
4)tellp()Function:
-It is used to get current position of put pointer.
-Syntax:
int tellp();
5)open()Function:
-It is used to open the file.
-Syntax:
open("file name",mode);
6)close()Function:
-It is used to close the file.
-Syntax:
close();
***File Reading And Writing MODE:
================================
=================================================================================
Lecture No 3.
/*
File Write
*/
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
ofstream fout;
fout.open("pune.txt",ios::out);
fout<<"\nHello World";
cout<<"\nFile Data Written successfully!!!";
getch();
}
=================================================================================
/*
File Read
*/
#include<iostream>
#include<conio.h>
#include<fstream>
void main()
{
clrscr();
char ch;
ifstream fin;
fin.open("pune.txt",ios::in);
while(!fin.eof())
{
fin.get(ch);
cout<<ch;
}
getch ();
}
=================================================================================
/*
File Read 1.
*/
#include<iostream>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
int count=0;
char ch;
ifstream fin;
fin.open("pune.txt",ios::in);
while(!fin.eof())
{
fin.get(ch);
if(ch==' ')
{
count++;
}
}
cout<<"\n No Of spaces present in file="<<count;
getch();
}
=================================================================================
/*
File Read 2.
*/
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
int count=0;
char ch;
ifstream fin;
fin.open("pune.txt",ios::in);
while(!fin.eof())
{
fin.get(ch);
if(ch=='z')
{
count++;
}
}
cout<<"\n No of times Alphabet 'z' present in file"<<count;
getch();
}
=================================================================================
/*
check how much time Alphabet 'A' present in file.
*/
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
int count=0;
char ch;
ifstream fin;
fin.open("pune.txt",ios::in);
while(!fin.eof())
{
fin.get(ch);
if(ch=='A')
{
count++;
}
}
cout<<"\n No of times Alphabet 'A' present in file "<<count;
getch();
}
=================================================================================
/*
copy the contents of one file to another file.
*/
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
char ch;
ifstream fin;
ofstream fout;
fin.open("pune.txt",ios::in)
fout.open("Nagar.txt",ios::out);
while(!fin.eof())
{
fin.get(ch);
fout<<ch;
}
cout<<"\n File Copied successfully!!!";
fin.close();
fout.close();
getch();
}
=================================================================================
Lecture No 4
Fig.stream class hierarchy
Functions:
1)eof() -This Function Will return true result if it reaches the end of the
file while accessing it otherwise false.
2)bad() -This Function return true if attempted unrecoverable errors
otherwise false.
3)good() -This Function return true if last operation was done successfully
otherwise false.
4)clear() -This Function clears all previous error states.
5)fail() -This Function return true if last operation was failed otherwise
false.
6)rdstate() -This Function return the status data of ios.
=================================================================================
Lecture No 5
/*
File Read
*/
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
ofstream fout;
clrscr();
fout.open("gpa.txt",ios::out);
fout<<"\nVjtech Academy!!!";
cout<<"\nFile Written successfully!!!";
fout.close();
getch();
}
=================================================================================
/*
File Read .
*/
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
ifstream fin;
char ch;
fin.open("gpa.txt",ios::in);
while(!fin.eof())
{
fin.get(ch);
cout<<ch;
}
fin.close();
getch();
}
=================================================================================
/*
File Read And Write.
*/
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
ifstream fin;
ofstream fout;
char ch;
clrscr();
fin.open("gpa.txt",ios::in);
fout.open("gpn.txt",ios::out);
while(!fin.eof())
{
fin.get(ch);
fout<<ch;
}
fin.close();
getch();
}
=================================================================================
/*
Emp File.
*/
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
ofstream fout;
int enpid;
char name[20];
float salary;
clrscr();
fout.open("vjtechsoft.txt",ios::app);
cout<<"\nEnter Employee ID:";
cin>>empid;
cout<<"\nEnter Employee Name:";
cin>>name;
cout<<"\nEnter Employee salary:";
cin>>salary;
/*
File Read .
*/
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
ifstream fin;
char ch;
clrscr();
fin.open("vjtechsoft.txt",ios::in);
while(!fin.eof())
{
fin.get(ch);
cout<<ch;
}
fin.close();
getch();
}
=================================================================================
/*
File Read .
*/
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
ifstream fin;
char ch;
clrscr();
fin.open("vjtechsoft.txt",ios::in);
fin.get(ch);
cout<<"\n"<<fin.good();
cout<<"\n"<<fin.bad();
cout<<"\n"<<fin.fail();
fin.close();
getch();
}
=================================================================================