减少从外部数据文件中检索字符数组的重复次数

时间:2013-10-21 07:00:30

标签: c++ arrays dynamic coding-style

我正在编写一个用于搜索外部数据文件(测试电子邮件)的课程。方法“fill_email”是将.txt中的字符提取到节点的字符数组中,直到字符“|”为止。出现。 1)我的代码需要一个临时指针,我想知道我是否可以跳过而直接将字符直接放入节点中? 2)我的代码还需要一个需要重复5次的while循环(每个字段一次),有什么方法可以做一个while循环,为我改变这些字段? 附:我的代码需要动态分配的数组,我确信将此方法放在我的代码的私有部分。

电子邮件结构的标题部分及其节点:

struct email { // struct for storing email data
    char * sent;    // date and time email was sent
    char * from;    // email address of sender
    char * to;      // email address of reciever
    char * subject;  // subject line of email
    char * contents; // contents of email
};

struct email_node { // node for storing email data and next
    email email_data;
    email_node * next;

};

从外部数据文件填充节点指针的实现。

int Classify::Fill_email(email_node* & node, char filename[]) {
    char* sent_temp = new char[25];  // These are the five temporary arrays for extracting from the 
    char* from_temp = new char[50];  // external data file and then strcpying to the dynamic character
    char* to_temp = new char[50];    // arrays in the node.
    char* subject_temp = new char[100]; 
    char* contents_temp = new char[500]; 
    int size = 0;

    ifstream source_file(filename);
    //I hate copy/pasting, but I really wasn't sure how to do this for each field otherwise.
    while(source_file.good()) {   // Reads until source_file is empty
        sent_temp[size] = source_file.get();             // It should extract the text from my external file "Sample_Emails.txt" 
        if (sent_temp[size] == '|')
            break;
        size++;                                       // and put them into a temporary array for the field
    }
    sent_temp[size] = '\0'; // So that the character array ends properly.
    size = 0; // Just for the next while loop
    //
    while(source_file.good())  // Goes on for 4 more variables copy pasted....

    email_head->email_data.sent = new char[strlen(sent_temp)]; // For initialziing the field

    strcpy(node->email_data.sent, sent_temp); // Copying the data from the temporary arrays to the fields in the node
    //
    delete sent_temp;
    //
    if(!(source_file.good()))  // If the external data file is empty, return 1
        return 1;
    return 0;
    //
};

1 个答案:

答案 0 :(得分:0)

根据我自己的建议,我首先要改变结构使用std::string而不是指针:

struct email { // struct for storing email data
    std::string sent;    // date and time email was sent
    std::string from;    // email address of sender
    std::string to;      // email address of reciever
    std::string subject;  // subject line of email
    std::string contents; // contents of email
};

然后我将结构(或指针)存储在std::vector

std::vector<email> emails;

然后阅读,我将使用std::getline(两者来读取文件中的行,并进行标记化),std::istringstream,如下所示:

int Classify::Fill_email(std::vector<email>& emails, const std::string& filename)
{
    std::ifstream email_file(filename);

    std::string line;
    while (std::getline(email_file, line))
    {
        std::istringstream email_line(line);

        email mail;

        std::getline(email_line, mail.sent, '|');
        std::getline(email_line, mail.from, '|');
        std::getline(email_line, mail.to, '|');
        std::getline(email_line, mail.subject, '|');
        std::getline(email_line, mail.contents);

        emails.push_back(mail);
    }
}