while循环加载文件不起作用

时间:2013-10-16 17:29:07

标签: c++ file-io while-loop ifstream

我正在尝试创建一个链接列表,按照收到的顺序添加来自文本文件的电子邮件。我用来检索电子邮件信息并创建新节点的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;
};

1 个答案:

答案 0 :(得分:1)

由于错误放置;

,你的循环空身
while(!(source_file.eof())); {
                           ^

由于没有代码可以设置流的eof标志,因此它变为无限循环,因此似乎永远不会执行后面的代码。