C ++通过子字符串验证电子邮件

时间:2017-09-12 18:10:39

标签: c++ validation email find substring

我正在尝试从一个目录中获取配置文件信息(用户名,电子邮件等)并将其放入另一个目录中。我一直在调试这个程序的代码,虽然没有错误,程序将无法运行,说该程序“已停止工作”。我已经在这个网站和其他人看过任何可能的答案,但没有找到。

#include <string>
#include <cstring>
#include <iostream>
#include <istream>
#include <ostream>
#include <fstream>
#include <iomanip>
#include <filesystem>
using namespace std;

class path{
public:
string parent_directory;
string root_directory;
};

class Data{
public:
string userName;
string nickName;
string fName;
string arena_FName;
string lName;
string arena_LName;
string email;
string arenaEmail;

friend std::istream& operator>>(std::istream& input, Data& d);
};
std::istream& operator>>(std::istream& input, Data& d){
std::getline(input, d.userName);
std::getline(input, d.nickName);
//...
std::getline(input, d.arenaEmail);
return input;
}

int main(){

ifstream myfile("myfunk.txt", ios::in);

ofstream arena("arena.txt");

myfile.open("myfunk.txt", ios::in);
if(myfile){
    cout << "Input file open." << endl;
}

arena.open("arena.txt", ios::out | ios::app);
if(arena){
    cout << "Output file open." << endl;
}

cout << "file opening test: success" << endl;

int x = 0;
int y = 4101;       //Total number of users in the directory.
int z = 0;          //For inputting the required lines of info for each profile.
int profile = 0;
bool valid = false;
string role;
//string arenaRole;
bool post = false;
string line;
string p = "This PC/..."; //Path to the folder of the individual pictures.
//myVar.save("...");
string p = "...";
path path1;
path root_directory;
path parent_directory;
//bool is_directory(const std::filesystem::path& p, std::error_code& ec) noexcept; //Checks if current location is a directory.
//bool postPic;
const unsigned int MAXIMUM_DATA = 4100u;
Data database[MAXIMUM_DATA];

cout << "All variables but the filesystem have been accepted!  Please install this program on the network." << endl;

while(x < y){
    cout << "Primary loop functioning" << endl;

    if(post = true){
        getline(myfile, line); //Grab and read next line.
        myfile >> line;
        line = userName[x];
        arena << "Username: " << userName[x] << "\n";
        z++;

        getline(myfile, line);
        myfile >> line;
        line = role[x];
        arena << "Role: " << role[x] << "\n";
        z++;

        getline(myfile, line);
        line = nickName[x];
        myfile >> nickName[x];
        arena << "nickname: " << nickName[x] << "\n";
        z++;

        getline(myfile, line);
        line = fName[x];
        myfile >> fName;
        arena << "First Name: " << fName[x] << "\n";
        z++;

        getline(myfile, line);
        line = lName[x];
        myfile >> lName;
        arena << "Last Name: " << lName[x] << "\n";
        z++;

        getline(myfile, line);
        myfile >> line;
        line = email[x];
        arena << "Email: " << email[x] << "\n";
        getline(myfile, line);
        z = 0; //Next profile...
    }

    int data;
    while(myfile >> data){
        if(nickName[x] = NULL){
            myfile >> "<Error> Some required information is missing! Contact user! </Error> /n";
            valid = false;
            post = false;
            x++;
            }

            if(email[x] != NULL){
                std::string str("@");
                std::string str2(".com");
                std::string str3(".net");
                std::string str4(".edu");
                if(std::size_t found = email[x].find(str) & (std::size_t found = email[x].find(str2) || std::size_t found = email[x].find(str3) || std::size_t found = email[x].find(str4)){
                    valid = true;
                    if(valid = true){
                        post = true;
                    }
                }

                else{
                    valid = false;
                    post = false;
                    x++;
                }
            }
        }
        }
    }

}
x++;
}

    //x++;

myfile.close(); //Closes the file in the directory.
arena.close();  //Closes the file in Arena.

return 0;
}

1 个答案:

答案 0 :(得分:0)

让我们重新修改你的代码 首先,让我们为数据创建一个数据结构:

class Data
{
  public:
    string userName;
    string nickName;
    string fName;
    string arena_FName;
    string lName;
    string arena_LName;
    string email;
    string arenaEmail;
};

如果您需要数据的数组,它将被声明为:

const unsigned int MAXIMUM_DATA = 4100u;
Data database[MAXIMUM_DATA];

接下来,让我们重载提取operator>>以使阅读更容易:

class Data
{
  public:
    //...
    friend std::istream& operator>>(std::istream& input, Data& d);
};
std::istream& operator>>(std::istream& input, Data& d)
{
  std::getline(input, d.userName);
  std::getline(input, d.nickName);
  //...
  std::getline(input, d.arenaEmail);
  return input;
}

这将您的输入循环简化为:

std::vector<Data> database;
Data d;
while (my_file >> d)
{
  database.push_back(d);
}

您可以使用std::vector::size()方法查询读入的数据量,即database.size()

此外,您不需要文件路径的单独结构。一个简单的std::string就足够了。我建议使用正斜杠'/',因为它可以被Windows和* nix操作系统识别,并且不会被解释为转义字符。