0% found this document useful (0 votes)
13 views12 pages

UNIT NO-5 File Operation

The document discusses file handling in C++. It covers topics like file streams, file opening modes, reading and writing to files, positioning pointers in files, and checking for errors during file operations. Example code is provided to demonstrate reading, writing, copying and manipulating file contents.

Uploaded by

21Siddhi Doke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views12 pages

UNIT NO-5 File Operation

The document discusses file handling in C++. It covers topics like file streams, file opening modes, reading and writing to files, positioning pointers in files, and checking for errors during file operations. Example code is provided to demonstrate reading, writing, copying and manipulating file contents.

Uploaded by

21Siddhi Doke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

VJTech Academy Vishal Sir…

 File Handling Lectures.


 Lecture No 1.
Fig…Input Output Operation.

=================================================================================

 Lecture No 2.
************File Operation****************
=========================================================
***Stream:
-It is a collection of continuous group of data.
-A Stream is sequence of bytes.

***File Management Function:


===============================
1)Seekg()Function:
-It is used to set the position of get pointer.

VJTech Academy ,contacts us :9730087674


VJTech Academy Vishal Sir…

-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:
================================

1)ios::in =>open file in read mode.


2)ios::out =>open file in write mode.
3)ios::app =>Append contents to the file.

VJTech Academy ,contacts us :9730087674


VJTech Academy Vishal Sir…

4)ios::ate =>Move the pointer to the end of file.


5)ios::binary =>File open in binary Mode.
6)ios::nocreate =>This will cause to fail the open function if file doesnt't
exists.
7)ios::noreplace =>This Will cause to fail open function if file is exists.
8)ios::trunc =>This Will truncate the file .

 Fig..Stream class Hierarchy.

=================================================================================

 Lecture No 3.
/*
File Write
*/
#include<iostream.h>
#include<fstream.h>
#include<conio.h>

VJTech Academy ,contacts us :9730087674


VJTech Academy Vishal Sir…

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>

VJTech Academy ,contacts us :9730087674


VJTech Academy Vishal Sir…

#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())
{

VJTech Academy ,contacts us :9730087674


VJTech Academy Vishal Sir…

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();
}
=================================================================================

VJTech Academy ,contacts us :9730087674


VJTech Academy Vishal Sir…

/*
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

 Exception and Error Handling During file Operation:


1.Attempting to access non-existing file.
2.Some disk Error.
3.Opening read only file in write mode.

VJTech Academy ,contacts us :9730087674


VJTech Academy Vishal Sir…

4.Opening file With an invalid name.


5.Reading beyond the end of file.

 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();
}
=================================================================================
/*

VJTech Academy ,contacts us :9730087674


VJTech Academy Vishal Sir…

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())
{

VJTech Academy ,contacts us :9730087674


VJTech Academy Vishal Sir…

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;

fout<<"\n"<<empid<<" "<<name<<" "<<salary;


cout<<"\nRecords submitted successfully!!!";
fout.close();
getch();
}
=================================================================================

/*

VJTech Academy ,contacts us :9730087674


VJTech Academy Vishal Sir…

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

VJTech Academy ,contacts us :9730087674


VJTech Academy Vishal Sir…

fin.close();
getch();
}
=================================================================================

VJTech Academy ,contacts us :9730087674

You might also like