2013-10-15 29 views
1

我必須編寫一個程序,該程序將從文件中提取電子郵件地址並將其放入另一個文件中。我不知道如何讓程序將信息放入其他文件中。另外,我是否必須創建第二個文件,就像我必須創建第一個文件一樣?以下是我迄今爲止:如何將提取的數據寫入outfile。程序旨在從文件中提取電子郵件地址C++

#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 
char chr; 

int main() 
{ 
string mail; 
ifstream inFile;        //this is the file that we will get the information from 
ofstream outfile;        // this is the file that the data will be saved in 
inFile.open("mail.dat");      // this will open the file with the original informations 
outfile.open("addresses.dat");    // this will open the file where the output will be 
while (inFile) 
{ 
    cin>>mail; 
    mail.find('@')!=string::npos;    //this finds the email addresses 

} 



inFile.close();        // this will close the file when we are done with it 
outfile.close(); 



cin>>chr; 
return 0; 
} 

回答

1

的問題是,提取應該while()循環的表達部分已經完成。而且,「找到電子郵件地址」的部分是無用的表達。您應該使用它作爲條件插入一個有效的電子郵件地址到輸出文件:

while (inFile >> mail) 
{ 
    if (mail.find('@') != std::string::npos) 
     outFile << mail; 
} 

在你的原代碼,您使用std::cin >> mail。我的印象是你對電子郵件地址已經存儲在輸入文件流中的問題的描述。如果該的情況下,你不應該使用std::cin而是​​執行提取。我上面做了修正。


下面是關於代碼質量的一些建議。你不應該在你的代碼中使用using namespace std。去掉它。這被認爲是不好的做法。相反,您應該使用std::來限定所有標準C++對象。

int main() 
{ 
    std::ifstream in; 
    std::ifstream out; 

    // ... 
} 

此外,兩個標準文件流對象都有一個構造函數,該構造函數接受文件的名稱。您仍然可以使用open,但它更方便從構造函數實例:

int main() 
{ 
    std::ifstream in("mail.dat"); 
    std::ofstream out("addresses.dat"); 

    // ... 
} 

你也應該使用標準庫算法做這樣瑣碎的事情。例如:

#include <iostream> 
#include <string> 
#include <fstream> 
#include <algorithm> 

int main() 
{ 
    std::ifstream in("mail.dat"); 
    std::ofstream out("addresses.dat"); 

    std::remove_copy_if(
       std::istream_iterator<std::string>{in}, 
       std::istream_iterator<std::string>{}, 
       std::ostream_iterator<std::string>{out, "\n"}, [] (std::string str) 
       { 
        return str.find('@') != std::string::npos; 
       }); 
} 
0

下面的代碼(從http://www.cplusplus.com/doc/tutorial/files/)展示瞭如何寫入文件:

// basic file operations 
#include <iostream> 
#include <fstream> 
using namespace std; 

int main() { 
    ofstream myfile; 
    myfile.open ("example.txt"); 
    myfile << "Writing this to a file.\n"; 
    myfile.close(); 
    return 0; 
} 

將此應用於您的情況,您需要在環行寫入到輸出文件。 「文件」實際上只是一個不同的「流」,就像控制檯一樣。您可能已經知道如何使用std::cout - 寫入文件幾乎完全一樣...

0

我不確定您希望程序如何工作,但這裏大致介紹瞭如何使用C++讀取和寫入文件:

#include <iostream> 
#include <fstream> 
#include <string> 

int main() 
{ 
    // Declare output file variable 
    std::ofstream myFile; 

    // Open/create text file 
    myFile.open("myDocument.txt"); 

    // Check if it's opened correctly (ie, not in use) 
    if(myFile.is_open()) 
    { 
     // Write to the file 
     myFile << "Line one.\n"; 
     myFile << "Line two.\n"; 
     // Close the file after use 
     myFile.close(); 
    } 
    else 
    { 
     // Output an error if we can't open it 
     std::cerr << "Could not open file."; 
    } 

    // Declare input file variable 
    std::ifstream readFile; 

    // Open this 
    readFile.open("myDocument.txt"); 

    // Check if it opened (ie, not in use) 
    if(readFile.is_open()) 
    { 
     // Temp variable to hold read lines 
     std::string temp; 

     // While not at the end of the document, get the line and store it in temp variable 
     while(std::getline(readFile, temp)) 
     { 
      // Output that line to the console 
      std::cout << temp << "\n"; 
     } 
     // Close the file 
     readFile.close(); 
    } 
    else 
    { 
     // Print an error if we couldn't open the file 
     std::cerr << "Could not open file."; 
    } 

    return 0; 
} 
0

電子郵件地址往往很複雜。你最初的方法尋找所有的互聯網電子郵件域地址會得到你很多,因爲它們的形式是,

[email protected] 

當頂級域名(TLD)是一個相當廣泛的一組值(COM,淨,埃杜, gov,us,uk,le,ly,de,so,ru,...)。最近,IANA宣佈取消對TLD價值的限制,所以你很快就會爆發新的TLD(蘋果,ibm,dell,att,cocacola,shell等)。

名稱部分可以是字母,數字和某些特殊字符。

您可能會發現使用正則表達式模式匹配庫將幫助您提取電子郵件地址。

這裏有幾個引用,這將有助於,

下面是一些例子維基百科給出了有效的電子郵件地址,

[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected][IPv6:2001:db8:1ff::a0b:dbd0] 
"much.more unusual"@example.com 
"[email protected]"@example.com 
[email protected] (top-level domains are valid hostnames) 
!#$%&'*+-/=?^_`{}|[email protected] 
"()<>[]:,;@\\\"!#$%&'*+-/=?^_`{}| ~.a"@example.org 
üñîçøðé@example.com (Unicode characters in local part) 
et cetera 

從文件中提取一個(或幾個)電子郵件地址後,您需要將每個電子郵件地址寫入您的outfile。假設emaddr包含一個有效的地址,

cout<<emaddr<<endl; //std::cout, std::endl if you don't 'using namespace std' 

我們不要忘記,還有其他的尋址方案,您可能希望瞭解,

等。

相關問題