Search code examples
c++fstream

fstream::write and fstream::read changing both reading and writing pointers


I am learning about how to write records on file and read it .
I created a class called student , the class has function like enterStudent , showStudent , printInsideFile (student info) ,...... .
when I try to write student info into the file , it works .
when I try to read all student info from the file , it works
when I try to do both , something unexpected happens (nothing show up)

i thought that the problem with file.flush() , so when i deleted it the output was unreadable text
i could close the file and open it again but i think that is silly
the code is:

#include <iostream>
#include <string>
#include <fstream>


using namespace std;

class student {
private:
    char id[5], name[20], age[5], address[50], gender[5];
public:
    void enterStudent() {
        cout << "Enter student id : "; cin >> id;
        cout << "\nEnter student name : "; cin >> name;
        cout << "\nEnter student age : "; cin >> age;
        cout << "\nEnter student address : "; cin >> address;
        cout << "\nEnter student gender : "; cin >> gender;

    }
    void showStudent() {
        cout << "#########student data##########\n";
        cout << "student id : "<< id;
        cout << "\nstudent name : " << name;
        cout << "\nstudent age : " << age;
        cout << "\nstudent address : " << address;
        cout << "\nstudent gender : " << gender<<endl;
    }
    void printInsideFile(fstream &file) {
        file.write(id,sizeof(id));
        file.write(name, sizeof(name));
        file.write(age, sizeof(age));
        file.write(address, sizeof(address));
        file.write(gender, sizeof(gender));
        file.flush();
    }
    bool readFromFile(fstream &file) {
        if (file.eof())
            return 0;
        file.read(id, sizeof(id));
        file.read(name, sizeof(name));
        file.read(age, sizeof(age));
        file.read(address, sizeof(address));
        file.read(gender, sizeof(gender));
        if (file.eof())
            return 0;
        return 1;
    }
    void showAllFromFile(fstream &file) {
        while (this->readFromFile(file)) 
            this->showStudent();
    }
};

int main() {
    student s;
    fstream file;

    file.open("a.txt", ios::in | ios::out | ios::app);
    if (!file)
        goto k270;
    
    s.enterStudent();
    s.printInsideFile(file);

    //when i read one student , it works okay

    //s.readFromFile(file);
    //s.showStudent();

    //when i try to read multiple students , it doesn't work at all
    s.showAllFromFile(file);




    file.close();


k270:
    system("pause");
    return 0;
}

Solution

  • The problem was not the ios::app

    It seems that functions file.write() and file.read() change either reading pointer and writing pointer .
    Even if you use file<<"text"; or file>>array of chars; both of pointers are changing .

    I searched about it but didn't find explanation but i find the code of ostream::write and istream::read ,they were advanced so if anybody check the link and tell us why

    note that ,when the reading pointer points at the end of the file , it will print out nothing