1

我正在尝试创建一个链接列表,按照收到的顺序从文本文件中添加电子邮件。我用来检索电子邮件信息和创建新节点的 while 循环甚至没有完成一次迭代(我用一个整数和另一个停止条件对其进行了测试)。我的 .txt 文件在与其他人的程序一起加载时工作,所以我相信我的 source_file.get 不工作?

int Classify::Load_email(char filename[]) {
    ifstream source_file(filename);
    char eof_prime;
    if(!source_file) // Checks whether there is anything in the file
        return 0;
    if(!email_head) { // If there is no head, creates a new node for it to point to, and have tail point to it as well
        email_head = new email_node;
        email_tail = email_head;
        email_tail->next = NULL;
    }
    source_file >> eof_prime; // Primes the eof function.

     while(!(source_file.eof())); {
        source_file.get(email_tail->email_data.sent, 50, '|');
        source_file.get(email_tail->email_data.from, 50, '|');
        source_file.get(email_tail->email_data.to, 50, '|');
        source_file.get(email_tail->email_data.subject, 100, '|');
        source_file.get(email_tail->email_data.contents, 200, '|');

        // If source_file isn't eof, then create a new node, connect the nodes, then have tail point to it, make next NULL
        if(!(source_file.eof())) {
            email_tail->next = new email_node;  // retaining the order emails were sent in, so always adding to the end
            email_tail = email_tail->next;
            email_tail->next = NULL;
        }
    } // end while loop
     return 1;
};
4

1 回答 1

1

由于放置错误,您的循环体为空;

while(!(source_file.eof())); {
                           ^

并且由于没有设置流eof标志的代码,它变成了一个无限循环,因此看起来后面的代码永远不会执行。

于 2013-10-16T17:34:42.067 回答