C++ Program to create a data file to store any ten rollnumbers and total marks and arrange the list in ascending order of marks.Read the file and print the new list obtained.
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
main()
{
clrscr();
// writing records in the data file
ofstream ftt("student.dat");
int marks,roll,i;
for (i=0;i<3;i++)
{
cout<<"Enter the details of record "<<i+1;
cout<<endl;
cout<<"Enter roll.number ";
cin>>roll;
cout<<endl;
cout<<"Enter the marks";
cin>>marks;
cout<<endl;
ftt<<roll<<"\n"<<marks<<"\n";
}
ftt.close();
// opening of the file in input mode
// for reading the records
ifstream fit ("student.dat");
int rno[10],mark[10];
for(i=0;i<3;i++)
{
fit>>roll;
fit>>marks;
rno[i]=roll;
mark[i]=marks;
cout<<endl<<"The record number is "<<i+1;
cout<<endl<<"The roll number is ";
cout<<roll;
cout<<endl<<"The marks are "<<marks;
cout<<endl;
}
cout<<"Press any key to sort the data ";
getch();
clrscr();
// sorting according to the marks.
int rnum,mar;
for(i=0;i<2;i++)
{
for(int j=i+1;j<3;j++)
{
if (mark[i]<mark[j])
{
mar=mark[i];
mark[i]=mark[j];
mark[j]=mar;
roll=rno[i];
rno[i]=rno[j];
rno[j]=roll;
}}}
fit.close();
// writing sorted records.
ofstream fft("student.dat");
for (i=0;i<3;i++)
{
fft<<rno[i]<<"\n"<<mark[i]<<"\n";
}
fft.close();
cout<<endl;
cout<<"Records re-written in the file. Press any key to continue";
getch();
clrscr();
// reading sorted records.
cout<<"The content of the list as read from the data file is : ";
cout<<endl<<"(In descending order of marks )";
cout<<endl;
ifstream fet ("student.dat");
for(i=0;i<3;i++)
{
fet>>rno[i];
fet>>mark[i];
cout<<endl<<"The record number is "<<i+1;
cout<<endl<<"The roll number is ";
cout<<rno[i];
cout<<endl<<"The marks are "<<mark[i];
cout<<endl;
} fet.close();
cout<<"Press any key to continue ";
getch();
return 0;
}
Related Posts : C++ Programs
0 comments:
Post a Comment