Create a structure Student containing fields for Roll No., Name, Class, Year and Total Marks. Create 2 students and store them in a file. Retrieve the student information from file created in previous question and print it in following format: Roll No. Name Marks:-





 #include<iostream.h>
#include<conio.h>
#include<fstream.h>

struct student
{
int rollno;
char name[20];
int cls;
int year;
int tm;
};
void main()
{
clrscr();
student v[2];
ofstream outfile("Student.txt");
for (int i=0;i<2;i++)
{
cout<<"Enter Roll Number:"<<endl;
cin>>v[i].rollno;
cout<<"Enter Name:"<<endl;
cin>>v[i].name;
cout<<"Enter Class:"<<endl;
cin>>v[i].cls;
cout<<"Enter year:"<<endl;
cin>>v[i].year;
cout<<"Enter total marks:"<<endl;
cin>>v[i].tm;
}
for (int j=0;j<2;j++)
{
outfile<<v[j].rollno<<" "<<v[j].name<<" "<<v[j].cls<<" "<<v[j].year<<" " <<v[j].tm<<endl;
}
outfile.close();
ifstream infile("Student.txt");
for (int c=0;c<2;c++)
{
infile>>v[c].rollno>>v[c].name>>v[c].cls>>v[c].year>>v[c].tm;
cout<<v[c].rollno<<" "<<v[c].name<<" "<<v[c].cls<<" " <<v[c].year<<" "<<v[c].tm<<endl;
}
getch();
}



Output:-

Comments