验证电子邮件地址和地址中的句点数

verifying email address and the number of periods in the address

本文关键字:句点 地址 电子邮件地址 验证      更新时间:2023-10-16

我想在业余时间学习c++,需要一些指导。我试图让用户输入一个包含电子邮件地址列表的文件。从这个列表中,我想检查每个电子邮件地址,说每个地址恰好包含一个句点。我想用一个包含指针的bool来解决这个问题。我在如何启动这个功能上遇到了麻烦。我成功地输入了文件,但下一步我输了。感谢您的帮助。谢谢

#include <iostream>
#include <fstream> 
#include <cstdlib>  
#include <cstring>  
using namespace std;

bool oneAt(const char *email);
bool nonblankAt(const char *email);
bool oneDot(const char *email);
bool nonblankDot(const char *email);
int main(){
    char filename[25];
    ifstream fin;
    cout << "Enter the input filen";
    cin >> filename;
    fin.open(filename);
    if(fin.fail()){
        cerr << "Input file opening error.n";
    }
    else{
        cout << "successn";
    }
    const int size = 50;
    char line[size];


    fin.close();
    system("Pause");
    return 0;
}
bool oneAt(const char *email)

如果你想学习C++,你更喜欢std::string而不是字符数组和指针:

bool oneAt(const std::string& email)
{
    return email.find('@') != email.end();
}
int main()
{
    std::string filename;
    cout << "Enter the input filen";
    if (std::cin >> filename)
    {
        if (std::ifstream fin(filename))
        {
            std::string line;
            while (getline(std::cin, line))
            {
                 if (oneAt(line))
                     std::cout << "found one @ in '" << line << "'n";
                 // similar for checking for periods etc..
            }
        }
        else
            std::cerr << "Input file opening error.n";
    }
    else
        cerr << "Error reading filenamen";
}

也就是说,验证电子邮件地址的任何看似严肃(可用于实际)的工作都最好使用正则表达式或自定义解析器来完成,这是出了名的复杂。谷歌"电子邮件验证的正则表达式"或类似的内容,你会发现激烈的讨论和不同复杂程度的许多变化。我怀疑任何人都不知道哪一个在最准确的整体报道方面是"最好的"。