Sunday, 31 August 2014

C++ program to count record in binary file

C++ program to count record in binary file:A simple file handling program which count the record in binary file.
Binary file:file stored in binary format. A binary file is computer -readable but not human-readable. All executable programs are stored in binary files, as are most numeric data files. In contrast, text files are stored in a form (usually ASCII) that is human-readable.

C++ program to count record in binary file



C++ program to count record in binary file


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


struct stud
{
char name[50];
int rno;
};

void main()
{


clrscr();
stud st;
int i;
ofstream gpj("abcabc.dat",ios::binary|ios::trunc);
if(!gpj)
{cout<<"File Cannot Be Created"<<endl;
}
else
{
int n;


cout<<"How many Students U Want to Enter ???? "<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter Name"<<endl;
gets(st.name);
cout<<"Enter Roll No"<<endl;
cin>>st.rno;
gpj.write((char*)&st,sizeof(st));
}gpj.close();
}
int a=0;
ifstream gp("abcabc.dat",ios::binary);
if(!gp)
{
cout<<"File Error"<<endl;
}
else
{
gp.seekg(0);
while(gp.read((char*)&st,sizeof(st)))
{
a++;
}
cout<<"No = "<<a;
gp.close();
}
getch();

}