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

Reducing repetition of retrieving character array from external data file

本文关键字:数组 字符 检索 从外部 数据 文件      更新时间:2023-10-16

我正在创建一个用于搜索外部数据文件(测试电子邮件)的类。这种方法"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] = ''; // 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;
    //
};

按照我自己的建议,我将首先更改结构以使用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);
    }
}