What is meant by the highlighted lines in the below program?

609 views Asked by At

The code here is being used for creating a Student Report card project but while trying to understand we can not figure out the use of and functions of the lines below from the code.

This:

File.read(reinterpret_cast<char *> (&st), sizeof(student));

and this:

int pos=(-1)*static_cast<int>(sizeof(st));

Here is the main code:

File.read(reinterpret_cast<char *> (&st), sizeof(student));
if(st.retrollno()==n)
    {
    st.showdata();
    cout<<"\n\nPlease Enter The New Details of student"<<endl;
        st.getdata();
            int pos=(-1)*static_cast<int>(sizeof(st));
            File.seekp(pos,ios::cur);
            File.write(reinterpret_cast<char *> (&st), sizeof(student));
            cout<<"\n\n\t Record Updated";
            found=true;
    }
1

There are 1 answers

2
Jean-François Fabre On
int pos=(-1)*static_cast<int>(sizeof(st));

converts the unsigned int type to integer and negates it, in order to compute the offset to seek backwards to in the next line

reinterpret_cast<char *> (&st)

just converts the pointer on the structure to a pointer on char so it's compatible with the function prototype. But the same pointer value is passed to the function.

So this code rewinds sizeof(st) bytes from the file and writes the new structure, updating the old one.