简体   繁体   English

任何人都可以向我解释这部分代码吗?

[英]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.语法后跟一个 if 语句,如下所示。 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.从文件中读取 a,如果成功 [1],检查found的仍然为零,并进入循环的其余部分。

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() . [1] 这里的成功是通过fp.read返回它所操作的istream对象来判断的,这可以转换为void * (pre-C++11) 或bool (C++11 以后)反映了fp.good()的状态。 In other words, the file is in a state where you can read more from it.换句话说,文件处于可以从中读取更多信息的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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