c++ - 在 C++ 中打开文本文件的输入验证
问题描述
我正在构建一个输入验证函数,它接受用户的输入并尝试打开该文件。如果用户输入的格式不正确,则重复。正确的格式是:
test1.txt
如果我在第一次运行时编写了正确的格式,我的函数就可以工作,但是在第二次运行之后,尽管我正在编写要打开的写入格式,但它会继续打印错误消息。我尝试在输入后清除输入“cin”和“filename”,但它不起作用。有任何想法吗 ?
string getFileInput()
{
string filename;
fstream file;
cout << "Please enter the name of the file: ";
getline(cin, filename);
file.open(filename.c_str());
while(!file.is_open())
{
file.clear(); file.ignore();
cout << "File name is incorrect, please enter again: ";
cin.clear(); cin.ignore();
getline(cin, filename);
file.open(filename.c_str());
}
// Extra condition. Empty file
if (file.eof())
{
cout << filename << " is an empty file." << endl;
}
file.close();
return filename;
}
解决方案
我可以复制和修复。
问题是由cin.ignore()
. 根据cppreference:
...输入序列中的下一个可用字符 c 是 delim
所以ignore
将阅读下一行,直到换行,然后不理会该换行。而下面getline
只能读取一个空字符串!
顺便说一句,对and使用未打开的fstream
( file
)至少是无用的,并且可能是有害的,因为这些方法预计将在打开的流上调用。而且使用也是无用的,并且可能是有害的:如果由于任何原因出现读取错误(例如,因为您到达文件末尾),您将始终清除错误条件并在应该中止时再次尝试读取。ignore
clear
cin.clear()
最后,仅在读取由于文件结尾而没有返回任何eof
内容后才设置条件。打开空文件时永远不会设置它,也不会成功读取文件末尾。
所以函数应该归结为:
string getFileInput()
{
string filename;
fstream file;
cout << "Please enter the name of the file: ";
getline(cin, filename);
if (! cin) {
// test the error immediately and before using filename!
cerr << "read error: aborting...\n";
return "";
}
file.open(filename.c_str());
while(!file.is_open())
{
cout << "File name is incorrect, please enter again: ";
getline(cin, filename);
if (! cin) {
// test the error immediately and before using filename!
cerr << "read error: aborting...\n";
return "";
}
file.open(filename.c_str());
}
file.close();
return filename;
}
推荐阅读
- c# - 位于另一个项目中的类由于其保护级别而无法访问
- amazon-web-services - Terraform 从通过 for_each 调用的资源中输出
- php - 如何在 Elementor SELECT2 控件上指定默认值或几个值?
- mysql - 更新值但返回错误 1242 - 子查询返回超过 1 行
- c# - Swagger UI 未显示 HTTPS 方案并且 SSL 证书已卸载(在负载均衡器上)
- excel - 将包含日期的文本拆分为列
- stripe-payments - 使用 Checkout 使用 Stripe 进行 12 个月付款
- azure-active-directory - 使用 azure 和 bot 框架为特定用户向团队 bot 添加身份验证
- node.js - 在 Heroku 上部署使用 Naked 包执行 Node.js 脚本的 Python 应用程序
- php - PHP HTML 多个 id 表单转换为模态体