0% found this document useful (0 votes)
271 views5 pages

text File Handling Program

This document discusses file handling in C++. It provides functions to create, add to, display, and count lines in a text file. It also provides functions to create, add records to, display all records, and search records in a binary file. The binary file functions use a struct to define student records with roll number, name, and marks fields. The main function uses a switch case to call the appropriate file handling function based on the user's menu choice.

Uploaded by

nita23arora2321
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
271 views5 pages

text File Handling Program

This document discusses file handling in C++. It provides functions to create, add to, display, and count lines in a text file. It also provides functions to create, add records to, display all records, and search records in a binary file. The binary file functions use a struct to define student records with roll number, name, and marks fields. The main function uses a switch case to call the appropriate file handling function based on the user's menu choice.

Uploaded by

nita23arora2321
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Computer Science (083) File Handling

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;
[Link]("[Link]",ios::out);
do
{
cout<<”Enter Text”;
gets(Lin);
Fil<<Lin<<endl;
cout<<”More(Y/N)?”;cin>>Q;
}
while (Q==’Y’);
[Link]();
}
}
void AddAtEnd() //Function to Add new lines to an existing
Text File
{
fstream Fil; char Lin[80],Q;
[Link]("[Link]",ios::app);
do
{
cout<<”Enter Text”;gets(Lin);
Fil<<Lin<<endl;
cout<<”More(Y/N)?”;cin>>Q;
}
while (Q==’Y’);
[Link]();
}
void Display() //Function to Display the content
{ //of a Text File
fstream Fil; char Lin[80];
[Link]("[Link]",ios::in);
while ([Link](Lin,80)) cout<<Lin<<endl;
[Link]();
}
int Count() //Function to Count the number
By Nita Arora 25
Computer Science (083) File Handling

{ //of lines in a Text File


fstream Fil;
[Link]("[Link]",ios::in);
char Lin[80];int Cnt=0;
while ([Link](Lin,80)) Cnt++;
[Link]();
return Cnt;
}

void main()
{
clrscr();
char Choice;
do
{
cout<<"The [Link] 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

#include<fstream.h> //** BINARY FILE HANDLING **


#include<stdio.h>
#include<conio.h>
struct stud
{ int rno;
char name[10];
int marks;
};
void create() // FUNCTION TO CREATE NEW FILE
{
stud rec;
fstream fl;
[Link]("[Link]", ios::out|ios::binary);
for (int i = 1;i<=4;i++)
{
cout <<"[Link]. : ";
cin>>[Link];
cout <<"name : " ;
gets([Link]);
cout <<"marks : " ;
cin>>[Link];
[Link]((char * )&rec,sizeof(rec));
}
[Link]();
}
void addatend() // FUNCTION TOADD NEW RECORDS
{ stud rec; // AT END OF THE FILE
fstream fl;
char Q;
[Link]("[Link]", ios::app | ios::binary);
do
{
cout <<"\nEnter The Record : \n\n";
cout <<"[Link]. : ";
cin>>[Link];
cout <<"name : " ;
gets([Link]);
cout <<"marks : " ;
cin>>[Link];
[Link]((char * )&rec,sizeof(rec));
cout <<"\nMore <Y/N>:";cin>>Q;
}
while(Q=='Y');
[Link]();
}

By Nita Arora 27
Computer Science (083) File Handling

void display() // FUNCTION TO DISPLAY ALL RECORDS


{
stud rec;
fstream fl;
[Link]("[Link]", ios::in);
[Link]((char * )&rec,sizeof(rec));
while (fl) //or while (![Link]())
{
cout <<"\[Link]. : "<<[Link];
cout <<"\nname : " <<[Link];
cout <<"\nmarks : "<<[Link];
[Link]((char * )&rec,sizeof(rec));
}
[Link]();
getch();
}
void search() // FUNCTION TO SEARCH RECORD
{ // FOR GIVEN ROLL NO.
stud rec;
fstream fl;
clrscr();
int r,found=0;
cout <<"Give Roll No To Be Searched : ";
cin >>r;
[Link]("[Link]", ios::in);
[Link]((char * )&rec,sizeof(rec));
while (fl)// while (![Link]())
{ if ([Link]==r)
{
cout <<"\[Link]. : "<<[Link];
cout <<"\nname : " <<[Link];
cout <<"\nmarks : "<<[Link];
found=1;

}
[Link]((char * )&rec,sizeof(rec));
}
if (found==0) cout <<"\nInvalid Roll No.";
[Link]();
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

You might also like