[英]Could anyone please explain me this part of a code?
我在学习文件处理时偶然发现了这种语法。
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;
}
}
许多文章只给出了这种比较的通用语法,但我找不到它的实际含义。 语法后跟一个 if 语句,如下所示。 谢谢!
这一行:
while(fp.read((char*)&st,sizeof(student)) && found==0)
从文件中读取 a,如果成功 [1],检查found
的仍然为零,并进入循环的其余部分。
我个人会这样做:
while(!found && fp.read(reinterpret_cast<char*>&st, sizeof(student))
反而。 这样,你就不会在写作后阅读额外的学生。
[1] 这里的成功是通过fp.read
返回它所操作的istream
对象来判断的,这可以转换为void *
(pre-C++11) 或bool
(C++11 以后)反映了fp.good()
的状态。 换句话说,文件处于可以从中读取更多信息的状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.