Question

I'm trying to make a linked list that adds emails from a text file in the order they were received. The while loop, that I'm using to retrieve email info and make new nodes, is not even completing a single iteration (I tested it with an integer and another condition for stopping). My .txt file worked when loading it with someone else's program, so I believe my source_file.get aren't working?

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;
};
Was it helpful?

Solution

Your loop has empty body because of mistakenly-placed ;

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

and since there is no code that would set the stream's eof flag, it becomes an infinite loop, therefore it seems that the code that follows is never executed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top