[英]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.