简体   繁体   中英

Could anyone please explain me this part of a code?

I stumbled on this syntax while learning file handling.

while(fp.read((char*)&st,sizeof(student)) && found==0)
{
    if(strcmpi(st.retadmno(),n)==0)
    {
        st.show_student();
        cout<<"\nEnter The New Details of student"<<endl;
        st.modify_student();
        int pos=-1*sizeof(st);
        fp.seekp(pos,ios::cur);
        fp.write((char*)&st,sizeof(student));
        cout<<"\n\n\t Record Updated";
        found=1;
    }
}

Many articles only gave the generic syntax of this comparison but i couldn't find the actual meaning of it. THe syntax is followed by an if statement and is as follows. Thanks!

This line:

while(fp.read((char*)&st,sizeof(student)) && found==0)

reads a from the file, and if that is successful [1], checks that found is still zero, and enters the rest of the loop.

I personally would do:

while(!found && fp.read(reinterpret_cast<char*>&st, sizeof(student))

instead. That way, you don't read an extra student after writing.

[1] The sucess here is judged by the fact that fp.read returns the istream object that it operates on, and this can be converted to void * (pre-C++11) or bool (C++11 onwards) which reflects the status of fp.good() . In other words, the file is in a state where you can read more from it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM