Thread: Linked list from file

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    134

    Linked list from file

    Below is the method implemented to read from the input file and make it a linked list, I can see that there are two entries inserted from end of the linked list when I diplay the linked list, kindly solve it

    I have called it from main() as :
    Code:
    parseFile(start, in); // start is the start pointer to the linked list
    Code:
    void parseFile(rectNode *record, ifstream &inFile){
            float coOrdinate;
            char comma;
            if(!inFile.eof())       {
                    inFile >> coOrdinate >> comma;
                    record->point1.x = coOrdinate;
    
                    inFile >> coOrdinate >> comma;
                    record->point1.y = coOrdinate;
    
                    inFile >> coOrdinate >> comma;
                    record->point2.x = coOrdinate;
    
                    inFile >> coOrdinate;
                    record->point2.y = coOrdinate;
    
                    record->next = (rectNode *) malloc(sizeof(rectNode));
    
                    cout << record->point1.x <<"yes ";
                    parseFile(record->next, inFile);
            }
            else    {
                    record = NULL;
            }
            return;
    }
    The input file is as:
    Code:
    0, 0, 10, 10
    2, 2, 8, 12
    339, 88, 444, 654

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You are leaking memory I believe. When the you reach EOF, record will be NULL, but the previous call of parseFile will have already allocated memory for it.

    This makes more sense:

    Code:
    void parseFile(rectNode *record, ifstream &inFile){
            float coOrdinate;
            char comma;
            if(!inFile.eof())       {
                    record = (rectNode *) malloc(sizeof(rectNode));
                    inFile >> coOrdinate >> comma;
                    record->point1.x = coOrdinate;
    
                    inFile >> coOrdinate >> comma;
                    record->point1.y = coOrdinate;
    
                    inFile >> coOrdinate >> comma;
                    record->point2.x = coOrdinate;
    
                    inFile >> coOrdinate;
                    record->point2.y = coOrdinate;                
    
                    cout << record->point1.x <<"yes ";
                    parseFile(record->next, inFile);
            }
            else    {
                    record = NULL;
            }
            return;
    }
    So, you allocate memory and initialize the node. If you reach EOF, you simply put NULL to the node, w/o having allocated memory for it.

    Also, why use malloc() in C++? Shouldn't you use new? Not that it really matters I guess

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kapil1089thekin
    Below is the method implemented to read from the input file and make it a linked list, I can see that there are two entries inserted from end of the linked list when I diplay the linked list, kindly solve it
    The problem may well be the incorrect use of eof() to control the recursion (similiar to the danger of using it to control a loop). I suggest changing the recursion to iteration and more properly testing as you read in, e.g.,
    Code:
    void parseFile(rectNode* record, ifstream& inFile) {
        char comma;
        record->next = NULL;
        while (inFile >> record->point1.x >> comma
                      >> record->point1.y >> comma
                      >> record->point2.x >> comma
                      >> record->point2.y) {
            record->next = static_cast<rectNode*>(malloc(sizeof(rectNode)));
            record = record->next;
            record->next = NULL;
        }
    }
    Of course, you need to test whether this properly handles invalid input

    Note that it is actually pointless to assign NULL to record in your original snippet since record is just a parameter that goes out of scope shortly thereafter. You might also want to consider creating an appropriate class type, and then using a std::list of that class type.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. Linked List from Standard Input
    By mercuryfrost in forum C Programming
    Replies: 14
    Last Post: 08-24-2009, 12:05 AM
  3. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM