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;
}
converts the
unsigned int
type to integer and negates it, in order to compute the offset to seek backwards to in the next linejust 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.