program To ENTER RECORDS Using Data File Handling
program To ENTER RECORDS Using Data File Handling
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
ofstream filout;
filout.open("marks.nik",ios::out);
char ans='y';
int rollno;
float marks;
clrscr();
while(ans=='y' || ans=='Y')
{
cout<<"\n ENTER ROLLNO. : ";
cin>>rollno;
cout<<"\n ENTER MARKS : ";
cin>>marks;
filout<<rollno<<'\n'<<marks<<'\n';
cout<<"\n WANT TO ENTER MORE RECORDS? (y/n)...";
cin>>ans;
}
filout.close();
}
//Program to ENTER 5 RECORDS Data file handling.
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
ofstream fout("student.nik");
char name[30],ch;
float marks=0.0;
for(int i=0;i<5;i++)
{
cout<<"STUDENT "<<i+1<<" : \t NAME : ";
cin.get(name,30);
cout<<"\t\t MARKS : ";
cin>>marks;
cin.get(ch);
fout<<name<<'\n'<<marks<<'\n';
}
fout.close();
ifstream fin("student.nik");
fin.seekg(0);
cout<<"\n";
for(i=0;i<5;i++)
{
fin.get(name,30);
fin.get(ch);
cout<<" STUDENT NAME : "<<name;
cout<<"\t MARKS : "<<marks<<"\n";
}
fin.close();
getch(); }
//Program to create a single file and then display its
contents
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
ofstream filout;
filout.open("stunames.nik");
filout<<"nitish karnatak\n"<<"nikki\n"<<"niks\n";
filout.close();
filout.open("stumarks.nik");
filout<<"99\n"<<"98\n"<<"97\n";
filout.close();
char line[80];
ifstream filin;
filin.open("stunames.nik");
cout<<"\n THE CONTENTS OF stunames.nik FILE ARE GIVEN BELOW :\n";
filin.getline(line,80);
cout<<line<<"\n";
filin.getline(line,80);
cout<<line<<"\n";
filin.getline(line,80);
cout<<line<<"\n";
filin.close();
filin.open("stumarks.nik");
cout<<"\n THE CONTENTS OF stumarks.nik FILE ARE GIVEN BELOW \n";
filin.getline(line,80);
cout<<line<<"\n";
filin.getline(line,80);
cout<<line<<"\n";
filin.getline(line,80);
cout<<line<<"\n";
filin.close();
getch();
}
//Program to use multiple files in succession.
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
ofstream filout;
filout.open("stunames.nik");
filout<<"nitish karnatak\n"<<"nikki\n"<<"niks\n";
filout.close();
filout.open("stumarks.nik");
filout<<"99\n"<<"98\n"<<"97\n";
filout.close();
ifstream filin1, filin2;
filin1.open("stunames.nik");
filin2.open("stumarks.nik");
char line[80];
cout<<" \n THE CONTENTS OF stunames AND stumarks ARE GIVEN BELOW : ";
filin1.getline(line,80);
cout<<line<<"\n";
filin2.getline(line,80);
cout<<line<<"\n";
filin1.getline(line,80);
cout<<line<<"\n";
filin2.getline(line,80); cout<<line<<"\n";
filin1.getline(line,80); cout<<line<<"\n";
filin2.getline(line,80); cout<<line<<"\n";
filin1.close(); filin2.close();
getch();
}
}
//Program to write & read a structure using write() & read()
struct customer
{
char name[40];
float balance;
};
void main()
{
clrscr();
customer savac;
strcpy(savac.name, " Sandeep Arora ");
savac.balance=21310.75;
ofstream fout;
fout.open("saving.nik" , ios:: out | ios::binary);
if(!fout)
{
cout<<"\n FILE CANNOT BE OPENED ....ABORTING !!";
getch();
exit(0);
}
getch();
}
//Program for reading & writing class objects
#include<fstream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
student stud;
fstream filin;
filin.open(" stumast.dat",ios::in | ios::out| ios :: binary);
if(!filin)
{
cout<<"CANNOT OPEN FILE!!\n";
getch();
exit(0);
}
int choice, mrec=0,offset=0;
char ans;
do
{
clrscr();
cout<<"\n\n\n\t\t MAIN MENU\n";
cout<<"\t\t________________\n";
cout<<"\n1.ADD RECORD.";
cout<<"\n2.MODIFY RECORD.";
cout<<"\n3.DISPLAY RECORD.";
cout<<"\n4.EXIT.";
cout<<"\n ENTER YOUR CHOICE...(1-4)";
cin>>choice;
switch(choice)
{
case 1 :
stud.getdata();
mrec=count;
offset=((mrec-1)*sizeof(student));
filin.seekp(offset,ios::beg);
filin.write((char*)&stud,sizeof(student));
break;
case 2:
if(!count)
{
cout<<"NO RECORD HAS BEEN ADDED.\n";
cout<<"PLEASE RUN OPTION 1 FIRST OF ALL. ";
gotoxy(10,23);
cout<<"PRESS ANY KEY TO CONTINUE...";
getch();
break;
}
cout<<"\n\n MODIFY WHICH RECORD ??#";
cin>>mrec;
if(mrec>count)
{
cout<<"\n\n\n ONLY "<<count<<" RECORDS HAVE BEEN ADDED.\n";
cout<<"INVALID RECORD NUMBER..\n";
gotoxy(15,23);
cout<<"PRESS ANY KEY TO CONTINUE...";
getch();
break;
}
else
{
offset=(mrec-1)*sizeof(student);
filin.seekg(offset,ios::beg);
filin.read((char*)&stud,sizeof(student));
stud.display();
cout<<"MODIFY THIS RECORD ? (y/n)...";
cin>>ans;
if(ans=='y' || ans=='Y')
{
cout<<"ENTER NEW DETAILS\n";
stud.mod_data();
filin.seekp(offset,ios::beg);
filin.write((char*)&stud,sizeof(student));
cout<<"\n RECORD MODIFIED\n";
gotoxy(30,24);
cout<<"\n PRESS A KEY TO CONTINUE..";
getch();
}
break;
}
case 3:
if(!count)
{
cout<<"NO RECORD HAS BEEN ADDED.\n";
cout<<"PLEASE RUN OPTION 1 FIRST OF ALL. ";
gotoxy(10,23);
cout<<"PRESS ANY KEY TO CONTINUE...";
getch();
break;
}
cout<<"\n\n DISPLAY WHICH RECORD ??#";
cin>>mrec;
if(mrec>count)
{
cout<<"\n\n\n ONLY "<<count<<" RECORDS HAVE BEEN ADDED.\n";
cout<<"INVALID RECORD NUMBER..\n";
gotoxy(15,23);
cout<<"PRESS ANY KEY TO CONTINUE...";
getch();
break;
}
else
{
offset=(mrec-1)*sizeof(student);
filin.seekg(offset,ios::beg);
filin.read((char*)&stud,sizeof(student));
stud.display();
gotoxy(10,23);
cout<<"\n PRESS ANY KEY TO CONTINUE..";
getch();
}
break;
case 4: break;
default : cout<<"WRONG CHOICE !! VALID CHOICES ARE 1-4.\n";
break;
}
}while(choice>=1 && choice<=3);
filin.close();
getch();
}
//A FILE NAMED MARKS.DAT ALREADY STORES SOME STUDENTS DETAILS.
WRITE A PROGRAM THAT READS MORE DETAILS AND APPENDS THEM TO THIS
FILE .
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
ifstream filin1,filin2;
filin1.open("stunames");
filin2.open("stumarks");
char line[80];
cout<<" \t The contents of stunames and stumarks are";
filin1.getline(line,80);
cout<<line<<"\n" ;
filin2.getline(line,80);
cout<<line<<"\n" ;
filin1.getline(line,80);
cout<<line<<"\n" ;
filin2.getline(line,80);
cout<<line<<"\n" ;
filin1.close();
filin2.close();
getch();
}
// PROGRAM TO APPEND DATA IN FILE
#include<fstream.h>
class stu
{
int rollno;
char name[25] ;
char clas[4];
float marks ;
char grade;
public:
void getdata()
{
cout<<" roll no";cin>>rollno;
cout<<" Name ";cin>>name ;
cout<<" class";cin>>clas;
cout<<"marks";cin>>marks;
if(marks>=75 )grade='A';
else if (marks>=60)grade='B';
else if (marks>=50)grade='C' ;
else if (marks>=40)grade='D';
else grade ='F';
}
void putdata()
{
cout<<name<<", rollno "<<rollno<<"has"<<marks<<"% marks and"
<<grade <<"grade"<< endl;
}
int getrno() //accessor function
{
return rollno;
}
}
s1;
void main()
{
ofstream fo("stu.dat",ios::app); char ans='y' ;
while (ans=='y')
{
s1.getdata();
fo.write((char*) & s1,sizeof(s1));
cout<<"record added to file ";
cout<<"want to enter more records";
cin>>ans;
}
fo.close();
}
//GET ROLLNOS & MARKS OF STUDENTS OF A CLASS AND STORE
THESE INTO A FILE CALLED MARKS.DAT
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
void main()
{
ofstream filout;
filout.open(" marks.dat",ios::out);
char ans='y';
int rollno,marks;
while( ans=='y')
{
cout<<"\n enter roll_no ";
cin>>rollno;
cout<<" \n enter marks";
cin>>marks;
filout<<rollno<<"\n"<<marks;
cout<<" want to enter more records( y/n ) ";
cin>>ans;
}
filout.close();
}
// PROGRAM TO DELETE A RECORD FROM FILE
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
class stu
{
int rollno;
char name[25];
char clas[4];
float marks;
char grade;
public :
if(marks>=60) grade='B';
else if(marks>=50) grade='C';
else if(marks>=40) grade='D';
else grade ='F';
}
void putdata()
{
cout<<"rollno"<<rollno<<"\tname:"<<name<<"\tgrade:"<<grade<<endl;
}
int getrno()
{
return rollno;
}
}s1,stud;
void main()
{
ifstream fio("stu.dat",ios::in);
ofstream file("temp.dat",ios::out);
int rno;
char found='f',confirm='n';
cout<<" enter rollno of student whose record is to be deleted ";
cin>>rno;
while (!fio.eof() )
{ fio.read((char*)&s1,sizeof(s1));
if (s1.getrno()==rno)
{ s1.putdata();
found='t';
cout<<"are you sure ,you want to delete this record";
cin>>confirm;
if(confirm=='n')
file.write((char*)&s1,sizeof(s1) ); }
else
file.write((char*)&s1,sizeof(s1));
}
if (found=='f')
cout<<"record not found";
fio.close();
file.close();
remove("stu.dat") ;
rename("temp.dat","stu.dat");
fio.open("stu.dat",ios::in);
cout<<"now the file contains \n";
while(!fio.eof())
{ fio.read((char*)&stud,sizeof(stud));
if(fio.eof())
stud.putdata();
}
fio.close(); }