2013-10-15 26 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' 

我们不要忘记,还有其他的寻址方案,您可能希望了解,

等。

相关问题