C++逐行读取文件并检查它是否等于特定字符串
C++ Reading File by line and checking if it equals to a specific string
我想创建一个程序,用户可以在其中输入一组特定的字符,然后检查已经创建的文件是否每行都有用户键入的完全相同的字符。
我的代码:
string line;
string employeeID;
bool found = false;
ifstream stream1("employees.txt", ios::app);
do{
cout << "Enter Employee Password:" << endl;
getline(cin, employeeID);
while (!stream1.eof())
{
getline(stream1, line);
if (employeeID == line){
found = true;
}
}
} while (found == false);
我的employees.txt文件包含:
ADH4172
DGGH481
换句话说,如果他们不输入ADH4172或DGHH481作为员工ID,我不希望它继续执行该程序。然而,上面的方法似乎不起作用。谢谢你的帮助
修复后的代码:
string line;
string employeeID;
bool found = false;
cout << "This portion of the program asks for the employee password, and checks its existence from a text files.nThe passwords are 5555 and 6666. Any other string will not workn" << endl;
do{
ifstream input("employees.txt", ios::app);
cout << "Enter Employee Password:" << endl;
getline(cin, employeeID);
while (!input.eof())
{
getline(input, line);
if (employeeID == line){
found = true;
input.close();
}
}
if (found == false){
cout << "Incorrect, try againn" << endl;
input.close();
}
} while (found == false);
while (found=false)
将found
设置为false
,使条件为false,因此循环立即终止。将其更改为"find==false"。
相关文章:
- C++LDAP检查用户是否是特定组的成员
- 检查是否以特定精度给出双精度
- 在Arduino中将PWM通道命名为LEDC是否有特定原因
- 如何在Qt 4.8中阻止/忽略/丢弃早于特定超时的用户输入事件
- 确保特征将 AVX 矢量化用于特定操作
- 确定夏令时是否适用于特定日期
- 非静态成员引用必须相对于特定对象
- 有没有办法搜索向量的元素,<String>然后检查它是否包含特定的字符,如果它确实打印了它
- 互斥锁能否保护相对于特定指针的数据?
- 依赖于特定类类型的C++模板方法
- 如何知道我正在使用的Linux OS是否具有特定的系统调用
- 检查元组是否包含特定类型的元素
- 如何判断类型是否是特定模板类的实例
- openMP 的 SIMD 结构是否需要特定类型的硬件?
- 将QT窗口强加于特定的屏幕
- 如何使函数模板仅适用于特定命名空间中的类型
- 不确定如何修复;非静态成员参考必须相对于特定对象
- "std::this_thread::sleep_for()"是否有特定于平台的限制
- 检查第 n 个可变参数模板参数是否属于特定类型
- UnookWindowsHookEx是否从特定于线程的挂钩应用程序中卸载库