Linked list from file

This is a discussion on Linked list from file within the C++ Programming forums, part of the General Programming Boards category; Below is the method implemented to read from the input file and make it a linked list, I can see ...

  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
    22,166
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 11:04 AM
  2. Linked List from Standard Input
    By mercuryfrost in forum C Programming
    Replies: 14
    Last Post: 08-24-2009, 01:05 AM
  3. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 08:53 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21