2017-09-12 62 views
-1

我試圖從一個目錄中獲取配置文件信息(用戶名,電子郵件等)並將其放入另一個目錄中。我一直在調試這個程序的代碼,雖然沒有錯誤,但程序不會運行,說程序「已停止工作」。我已經看過這個網站和其他人的任何可能的答案,並沒有發現。C++通過子字符串驗證電子郵件

#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; 
} 
+0

歡迎來到Stack Overflow,@ Sora-Knight。您可能想要查看:https://stackoverflow.com/help/how-to-ask通過此信息和其他幫助鏈接,您可以編輯此問題,以便其他人能夠爲您提供一個很好的答案。特別是,你應該將這個問題簡化爲一個問題,隨時輸入其他問題。您應該發佈簡潔地顯示您遇到的一個問題問題的代碼:https://stackoverflow.com/help/mcve – Degan

+0

無關:您已將它註釋掉,但'while(!myfile.eof())'會引導您悲傷。這裏解釋:[爲什麼iostream :: eof內部循環條件被認爲是錯誤的?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301

+0

我不喜歡我的C++ 17,但使用命名空間std :: experimental :: filesystem :: v1;'可能會咬你。 'filesystem'有一個'path'類,你已經把它拉入全局名字空間並與你的'path'類衝突。 – user4581301

回答

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操作系統識別,並且不會被解釋爲轉義字符。

+0

非常感謝您的幫助!由於實施了一些編碼,錯誤已經開始消失!在z變量上,我使用它來逐行讀取目錄中的每個文件,並且z變量的值確定該行將歸檔到哪個類別下。另外,在輸入循環中,我需要它只輸入有效的配置文件信息。此外,一些配置文件有一些信息缺失,雖然需要,用戶根本無法提供,即沒有電話號碼或電子郵件。因此,恐怕我無法簡化代碼。 –

+0

@ user4581301,謝謝你提及'#include '部分,因爲我沒有意識到這是一個錯誤。我現在修復了它。 –