A program to enter the Roll No., Name and Marks obtained by a student in a Binary File.

 

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

void main()
{
clrscr();

struct student
{
int rollno;
char name[23];
float marks;
}s1,s2;

ofstream wfile;
wfile.open("student.dat");

cout<<"Enter Roll no. of student: ";
cin>>s1.rollno;
cout<<"Enter name of student: ";
gets(s1.name);
cout<<"Enter marks obtained by student: ";
cin>>s1.marks;

wfile.write((char *)&s1, sizeof(s1));
wfile.close();

ifstream yfile;
yfile.open("student.dat");
yfile.read((char *)&s2,sizeof(s2));

while(yfile)
{
yfile.read((char *)&s2,sizeof(s2));
cout<<"\nData read from the file: "<<endl;
cout<<"Name: "<<s2.name<<endl;
cout<<"Roll no.: "<<s2.rollno<<endl;
cout<<"Marks obtained: "<<s2.marks<<endl;
}
yfile.close();
getch();
}

 

< Back