//C++ Prorgram to insert data in a sorted file
#include<iostream.h>
#include<ftream.h>
#include<stdio.h>
class stu
{
int rollno;
char name[25];
char class[4];
float marks;
char grade;
public:
void getdata()
{
cout<<"Roll Number:";
cin>>roll no;
cout<<"Name:";
cin>>name;
cout<<"Class:";
cin>>class;
cout<<"Marks:";
cin>>marks;
if(marks>=75)
grade='A';
elseif(marks>=60)
grade='B';
elseif(marks>=50)
grade='C';
elseif(marks>=40)
grade='D';
else grade='F';
}
void putdata()
{
cout<<"Roll Number"<<rollno<<"\tName:"<<name<<"\nMarks:"<<marks<<"\tGrade:"<<grade<<endl;
}
int getrno()
{
return rollno;
}
s1,stud;
int main()
{
ifstream fi("stu.dat",ios::in);
ofstream fo("temp.dat",ios::out);
char last='y';
cout<<\t eongo\n";
cout<<"www.eongo.co.cc"
cout<<"www.eongo.tk"
cout<<"www.eongo.blogspot.com\n";
cout<<"Enter the details of the student whose record is to be inserted\n";
s1.getdata();
while(!fi.eof())
{
fi.read((char*)&stud,sizeof(stud));
if(s1.getrno()<=stud.getrno())
{
fo.write((char*)&s1,sizeof(s1));
last='n';
break;
}
else
fo.write((char*)&stud,sizeof(stud));
}
if(last=='y')
fo.write((char*)&s1,sizeof(s1));
else if(!fi.eof())
{
while(!fi.eof())
{
fi.read((char*)&stud,sizeof(stud));
fo.write((char*)&stud,sizeof(stud));
}
}
fi.close();
fo.close();
remove("stu.dat");
fi.open("stu.dat");
rename("temp.dat","stu.dat";
fi.open("stu.dat",ios::in);
cout<<"File now contains\n";
while(!eof())break;
stud.putdata();
}
fi.close();
return 0;
}
No comments:
Post a Comment