I want to know that how many Student class objects in a file.
plz help me out.
Below is a class of Students and 2 function for read and write object in a file, in read function
while(in.eof()!= 1){
in.read((char *)&st,sizeof(st));
st.showData();
}
st.showData(); always show the last object twise.. unable to sort out my mistake in the code.
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string.h>
using namespace std;
class Student{
private:
char name[30];
int id;
int rollNo;
public:
Student(){}
Student(char *n,int i, int r){
strcpy_s(name,n);
id = i;
rollNo = r;
}
void getData(){
cout << "Enter Name : "; cin >> name;
cout << "Enter Id : "; cin >> id;
cout << "Enter Roll No : "; cin>> rollNo;
}
void showData(){
cout << "Student Detail---------------"<<endl;
cout << "Name : " << name << endl;
cout << "Id Number : " <<id << endl;
cout << "Roll No : " <<rollNo << endl;
}
};
void outData(Student &st){
ofstream out;
out.open("obj.txt",ios::app | ios::binary);
out.write((char *)&st,sizeof(st));
out.close();
}
void inData(Student st){
int count =0;
ifstream in;
in.open("obj.txt",ios::in|ios::binary);
while(in.eof()!= 1){
in.read((char *)&st,sizeof(st));
st.showData();
}
in.close();
}
int main()
{
Student st;
inData(st);
return 0;
}