C++文件处理,如何实现在此代码上找不到记录
C++ File Handling , How to implement record not found on this code
当用户尝试搜索数据文件中不可用的内容时,如何使用cout
消息向用户输出?我尝试了以下代码:
cin>>temp;
fstream a;
a.open("Account.dat",ios::app|ios::in|ios::out);
while(a>>acno>>type>>cid>>credit>>debit>>tellid){
if(temp==acno){
cout<<"Customer ID : "<<cid<<endl;
break;
}//if not found , display an error message,I've tried using else block here
}
谢谢!
您不能在循环中使用if - else
,因为每次读取帐户与搜索条件不匹配时都会执行此操作。相反,使用一个变量来跟踪我们是否发现了某些东西:
cin>>temp;
fstream a;
a.open("Account.dat",ios::app|ios::in|ios::out);
bool found = false;
while(a>>acno>>type>>cid>>credit>>debit>>tellid){
if(temp==acno){
cout<<"Customer ID : "<<cid<<endl;
found = true;
break;
}
}
if(!found) {
// display error message and exit
}
顺便说一句,这不是 iostream 的预期目的。相反,请使用像 Boost.PropertyTree 这样的库来保存数据。这将大大简化您的代码。
相关文章:
- 正在将DJI错误代码记录到流中
- 如何在代码中启用/禁用 spdlog 日志记录?
- 记录组件代码执行
- 从接收中获取未记录的错误代码
- 我不想导出的函数的未记录代码对象警告
- 无法链接提升日志记录代码
- 记录代码标头和源代码
- 使用 noweb 和 LaTeX 记录 C 或 C++ 代码
- 是否有一个工具来记录代码执行
- 自我记录代码是否值得潜在的性能问题
- 有时webservice会返回1000条记录.如何在代码中处理
- 以维基媒体格式或至少以HTML格式记录您的c / c ++代码
- 记录C++代码的最佳位置
- c++ system()调用没有正确记录Java返回代码
- 记录代码所花费的时间
- 加速数据记录代码
- c++宏记录每一行代码
- C++文件处理,如何实现在此代码上找不到记录
- c++代码访问UniVerse数据库记录的一种方法
- 你认为是什么让这个C++代码变慢了?(它循环通过ADODB记录集,将COM类型转换为字符串,并填充ostringstre