text File Handling Program
text File Handling Program
Text Files
#include <fstream.h>
#include <conio.h> //Text File Handling Program
#include <ctype.h>
#include <stdio.h>
void Create() //Function to create new Text File
{
char Ch;
cout<<"Delete the existing data(if any),Are You Sure (Y/N)?";cin>>Ch;
if (toupper(Ch)=='Y')
{
fstream Fil; char Lin[80],Q;
Fil.open("STUD.TXT",ios::out);
do
{
cout<<”Enter Text”;
gets(Lin);
Fil<<Lin<<endl;
cout<<”More(Y/N)?”;cin>>Q;
}
while (Q==’Y’);
Fil.close();
}
}
void AddAtEnd() //Function to Add new lines to an existing
Text File
{
fstream Fil; char Lin[80],Q;
Fil.open("STUD.TXT",ios::app);
do
{
cout<<”Enter Text”;gets(Lin);
Fil<<Lin<<endl;
cout<<”More(Y/N)?”;cin>>Q;
}
while (Q==’Y’);
Fil.close();
}
void Display() //Function to Display the content
{ //of a Text File
fstream Fil; char Lin[80];
Fil.open("STUD.TXT",ios::in);
while (Fil.getline(Lin,80)) cout<<Lin<<endl;
Fil.close();
}
int Count() //Function to Count the number
By Nita Arora 25
Computer Science (083) File Handling
void main()
{
clrscr();
char Choice;
do
{
cout<<"The no.of Lines in file:"<<Count()<<" Objects ..."<<endl;
cout<<"C:Create/A:Addatend/D:Display/Q:Quit"<<endl;cin>>Choice;
switch(Choice)
{
case 'C':Create();break;
case 'A':AddAtEnd();break;
case 'D':Display();break;
}
}
while (Choice!='Q');
}
By Nita Arora 26
Computer Science (083) File Handling
Binary Files
By Nita Arora 27
Computer Science (083) File Handling
}
fl.read((char * )&rec,sizeof(rec));
}
if (found==0) cout <<"\nInvalid Roll No.";
fl.close();
getch();
By Nita Arora 28
Computer Science (083) File Handling
void main()
{
char choice;
do{
clrscr();
cout<<"\n\n\tC:Create\n\tA:Add at end\n\tD:Display";
cout<<"\n\tS:Search\n\tQ:Quit\n\t";cin>>choice;
switch(choice)
{
case 'C' : create();break;
case 'A' : addatend();break;
case 'D' : display();break;
case 'S' : search();break;
}
}
while(choice!='Q');
}
By Nita Arora 29