我必須在名爲record.txt的文件中搜索字符串look_for,但代碼不起作用。如何搜索文件中的字符串並打印包含該字符串的行?
每次我給的值look_for是目前它說沒有找到記錄
string look_for, line;
in.open("record.txt");
cout<<"what is registration no of student ?";
cin>>look_for;
while(getline(in,line))
{
if(line.find(look_for)!= string::npos)
{
cout<<" record found "<<endl<<endl;
break;
}
else cout<<"record not found ";
}
請發表[mcve]。 –
...應該包含一個簡短的示例文件和一個'look_for'值。 – Rook
(首先明確檢查:'in'在你調用'open'後仍然有效,對吧?) – Rook