使用C++根据行中的第一个字符串查找行(仅一个)
Find line (one only) based on first string in line using C++
目标:通过搜索电子邮件地址,在数据文件中找到正确的行进行调整
问题:根据下面的当前代码,当我使用string::find
时,我可以找到这一行,但当我搜索rick@fakedomain.tld
时,我得到了两个命中,即frederick@
和rick@
行。由于电子邮件地址是第一个字段,我不能包含前导空格(分隔符)来解决这个问题。
示例输入数据文件(称为数据文件):
alan@fakedomain.tld q7gPGAdb0zGKHlQd./ Alan Smith
frederick@fakedomain.tld cyHSYctfJpOq7gPGAd Frederick Smith
david@fakedomain.tld nz0hz1uevogQgNxqQA David Smith
rick@fakedomain.tld 4bExd5J3tU7Pi9o/My Rick Smith
john@fakedomain.tld q7gPGAdb0zGKHlQd./0 John Smith
当前c++代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
// Are there the right number of command line arguments
if (argc < 2) { // expecting 2, the command and email address
cout << "test [emailaddress]" << endl;
return 1;
}
if (argc > 2) {
cout << "test: to many arguments" << endl;
return 1;
}
// put the command line arguments into string variables
string cmd = argv[0];
string email = argv[1];
// display the command line for testing - remove secton for production
string space = " ";
string cmdline = cmd + space + email + space + oldPW + space + oldPW;
cout << cmdline << endl;
// Open the files
ifstream filein("datafile"); //File to read from
if(!filein)
{
cout << "Error opening file!" << endl;
return 1;
}
string strLine;
while(getline(filein, strLine))
{
// the test to see if this is the line to work with
size_t found = strLine.find(email);
if (found != string::npos) {
// this is the string so add code here to work with it
cout << strLine << endl;
}
}
return 0;
}
问题:如何只返回与针头完全匹配的线路?
如果它要求不高,希望得到一个教会我的答案,而不仅仅是一些有效的未注释代码,如果我能得到的只有后者,它会接受后者。
可选:如果有更好的方法来完成您看到的代码的其他部分,请随时在您的回答或评论中提出建议。总是热衷于学习更好的方法。
所需输出
./test rick@fakedomain.tld
alan@fakedomain.tld q7gPGAdb0zGKHlQd./ Alan Smith
- - no match
frederick@fakedomain.tld cyHSYctfJpOq7gPGAd Frederick Smith
- - no match
david@fakedomain.tld nz0hz1uevogQgNxqQA David Smith
- - no match
rick@fakedomain.tld 4bExd5J3tU7Pi9o/My Rick Smith
|=|=| MATCH
john@fakedomain.tld q7gPGAdb0zGKHlQd./0 John Smith
- - no match
我得到的输出
./test rick@fakedomain.tld
alan@fakedomain.tld q7gPGAdb0zGKHlQd./ Alan Smith
- - no match
frederick@fakedomain.tld cyHSYctfJpOq7gPGAd Frederick Smith
|=|=| MATCH
david@fakedomain.tld nz0hz1uevogQgNxqQA David Smith
- - no match
rick@fakedomain.tld 4bExd5J3tU7Pi9o/My Rick Smith
|=|=| MATCH
john@fakedomain.tld q7gPGAdb0zGKHlQd./0 John Smith
- - no match
注意:使用g++ test.c -o test
没有编译错误或警告
如果您想确保找到的电子邮件位于字符串的开头,那么只需检查以确保找到的是零。
if (found == 0)
cout << strLine << endl;
另一种选择是在文件的行上使用std::string::substr
,并与operator ==
进行直接比较
if (email == strLine.substr(0, email.size()))
cout << strLine << endl;
相关文章:
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 试图创建一个多线程程序来查找0-100000000之间的总素数
- 一个函数,用于查找字符串1包含字符串2 c++的次数
- 如何在 C++11 中查找和更新向量中的一个嵌套结构
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- 用于在 C++ 中使用 while 循环查找下一个素数的简单函数
- 查找最小的下一个更大的元素
- 查找不在标准中的第一个值::设置<int>最小-最大值
- 编译器在C++中调用另一个函数时,在参数中查找已删除的构造函数
- 使用一个内存集数组和单个堆栈在 O(n) 中查找数组的下一个更大元素
- 尝试创建一个程序来查找非二叉树的高度.最终得到一个很长的循环,没有答案
- 如何制作一个查找数字平方值的 c++ 程序
- 查找下一个具有真值C++的数组索引
- 检查IP是否在其他IP网络范围内,并查找下一个可用IP
- 查找 2D 向量的位置,该向量是类C++的一个元素
- 将两个双数组集组合为一个查找其并集和交集的最简单方法
- 是因为两个查找而不是一个查找,所以速度变慢了吗
- 在c++中实现一个查找和替换过程