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

C++ 减少从外部数据文件检索字符数组的重复,c++,arrays,dynamic,coding-style,C++,Arrays,Dynamic,Coding Style,我正在制作一个用于搜索外部数据文件(测试电子邮件)的类。其中的“fill_email”方法是将.txt中的字符提取到节点的字符数组中,直到出现字符“|”。 1) 我的代码需要一个临时指针,我想知道是否可以跳过它,直接将字符输入节点? 2) 我的代码还需要一个while循环,该循环需要重复5次(每个字段一次),是否有某种方法可以执行while循环来更改这些字段? 另外,我的代码需要动态分配数组,我肯定会把这个方法放在代码的私有部分 电子邮件结构及其节点的标题部分: struct email { /

我正在制作一个用于搜索外部数据文件(测试电子邮件)的类。其中的“fill_email”方法是将.txt中的字符提取到节点的字符数组中,直到出现字符“|”。 1) 我的代码需要一个临时指针,我想知道是否可以跳过它,直接将字符输入节点? 2) 我的代码还需要一个while循环,该循环需要重复5次(每个字段一次),是否有某种方法可以执行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;
    //
};

按照我自己的建议,我首先将结构更改为使用而不是指针:

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
};
然后我将结构(或指针)存储在:


不要对字符串使用动态分配的
char
指针。使用
std::string
。此外,在(file.good())或
while(!file.eof())
时不要循环
。在尝试读取实际失败之前,不会设置错误或EOF标志,这意味着您可能会循环一对多,然后读取失败导致您添加错误的数据。哦,与其创建自己的列表,为什么不简单地使用例如
std::vector
?哦,当您分配
strlen时,您的代码中有一个off by one错误(sent_temp)
字符串字节,您忘记为字符串终止符分配空间。
std::vector<email> emails;
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);
    }
}