Problem reading a delimited file into a binary tree

This is a discussion on Problem reading a delimited file into a binary tree within the C++ Programming forums, part of the General Programming Boards category; I'm trying to read in a delimited file to my program and insert the contents into a binary tree. Here's ...

  1. #1
    &operator overload neolyn's Avatar
    Join Date
    Nov 2004
    Location
    Morgantown, WV
    Posts
    16

    Problem reading a delimited file into a binary tree

    I'm trying to read in a delimited file to my program and insert the contents into a binary tree. Here's what I've got:

    Code:
        char *ffirst;
        char *flast;
        char *fcity;
        char *fstate;
        char *fzip;
        fstream inf;
        
        inf.open(data, ios::in);
        while(!inf.eof()) {
             
            inf.getline(ffirst, 15, '|');
            inf.getline(flast, 20, '|');  /// ERROR ON EXECUTE - CRASHES HERE
            inf.getline(fcity, 25, '|');
            inf.getline(fstate, 2, '|');
            inf.getline(fzip, 10, '\n');
            
            system("PAUSE");
            insert(ffirst, flast, fcity, fstate, fzip); // Insert data into b-tree
        }

    The program will always crash on me after it reads the first delimited element in the file. I can't find the problem with the code- maybe there's some logic about file I/O that I'm missing here - please help.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    I believe you need to use the get function in this case.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You didn't set aside memory to hold the data. Your char* variables are just pointers that don't point to anywhere. Either dynamically allocate memory to hold the data, or create the character arrays on the stack like:

    char ffirst[15];
    char flast[20];
    etc...



    On a side note, your eof() check is wrong, you must check eof() after you attempt to read using getline, so you will run through your loop one extra time and insert the last person's data twice.

  4. #4
    &operator overload neolyn's Avatar
    Join Date
    Nov 2004
    Location
    Morgantown, WV
    Posts
    16
    Could you please explain the "get function"?

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You do not need the get function in this case. (see my above post).

  6. #6
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Ugh, sorry, I'm on crack today, I can't believe I missed that...

    *gauges eyes out*

    Disregard my post, I'm really out of it today

  7. #7
    &operator overload neolyn's Avatar
    Join Date
    Nov 2004
    Location
    Morgantown, WV
    Posts
    16
    i was able to get it working once I stopped using pointers. So, I am still ahveing trouble iwth the eof()q

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by neolyn
    i was able to get it working once I stopped using pointers. So, I am still ahveing trouble iwth the eof()q
    Smart move.

    To fix the eof problem, you can put the first getline into the while statement, since the return value of getline is converted to true or false based on whether it was successful or not. You could also put the first getline (or all of them if your data might be bad) into an if statement and break out of the while loop if it returns false:
    Code:
    if (!inf.getline(ffirst, 15, '|'))
        break;

  9. #9
    &operator overload neolyn's Avatar
    Join Date
    Nov 2004
    Location
    Morgantown, WV
    Posts
    16
    Yeah - I tried that - not working. lol

    Code:
        while(1) {
             
            inf.getline(ffirst, 15, '|');
            inf.getline(flast, 20, '|');
            inf.getline(fcity, 25, '|');
            inf.getline(fstate, 2, '|');
            inf.getline(fzip, 10, '\n');
            
            insert(ffirst, flast, fcity, fstate, fzip); // Insert data into tree
    
            if(!inf.getline(ffirst, 15, '|')) {
                    break;
            }
        }

  10. #10
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Do you realize that by simply pasting that code in you are reading the ffirst name twice? The point is that every time you read in a new set of data, it is possible that the last set of data you read in was the last set of data in the file. So the very first thing you attempt to read in when going through the loop again is when you should be checking to see if that read succeeded or not.

    See if you can figure it out.

  11. #11
    &operator overload neolyn's Avatar
    Join Date
    Nov 2004
    Location
    Morgantown, WV
    Posts
    16
    Thanks for your help. I appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree Search
    By C++Newbie in forum C++ Programming
    Replies: 7
    Last Post: 04-05-2011, 02:17 AM
  2. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

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