dimanche 3 mai 2020

program reading the data twice from the file in C++ [duplicate]

I was trying to read the data from the file "fainfo.txt" that stores a user-defined object. When I read the object from the file, the object is read two time before end of file function becomes true.

class Admin
{
    public:
    string name;
    int totalSubjects;
    string subject[10];
    string mobile, mail, fatherName, password, recoveryPassowrd;
    string rollNo;
    void setFacultyData();//Takes input from the user
    void getFacultyData();//prints the information that is received from the file
}a;
int main()
{
    fstream file;

    file.open("fainfo.txt", ios::out|ios::ate);

    a.setFacultyData();

    file.write((char *)&a, sizeof(a));
    file.close();

    file.open("fainfo.txt", ios::in);
    file.seekg(0);

    while (!file.eof())
    {
        file.read((char *)&a, sizeof(a));
        a.getFacultyData();
    }

    file.close();

    return 0;
}

Output for the program

Aucun commentaire:

Enregistrer un commentaire