Thread: Trouble reading from file

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

    Question Trouble reading from file

    I need to read this delimited file into a program using a loop. Each iteration of the loop. the data will be inserted into a tree data structure, but the way I have the loop written, it runs infinately. Please help.

    File contents:
    Code:
    firstName|lastName|City|State|Zip\n
    Code:
        while(!inf.eof()) {
    
            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 binary tree
    
        }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Several things
    1. how did you declare ffirst etc
    2. Have you read the FAQ about why eof() is not the best way to detect when you've read a complete file
    3. Where are your error checks on each of your getline calls.

  3. #3
    &operator overload neolyn's Avatar
    Join Date
    Nov 2004
    Location
    Morgantown, WV
    Posts
    16
    Code:
        char ffirst[15];
        char flast[20];
        char fcity[25];
        char fstate[2];
        char fzip[10];
    I'm really just unsure as to why this isn't working. It should just terminate the loop at the end of the file.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > char fstate[2];
    Well take states for an example "CA"
    This takes 3 chars - two for the two letters, and one for the \0

    Just to make it easier to change, try
    Code:
    inf.getline(fstate, sizeof fstate, '|');

  5. #5
    &operator overload neolyn's Avatar
    Join Date
    Nov 2004
    Location
    Morgantown, WV
    Posts
    16
    I see that. But why isn't inf.eof() detecting that I'm at the end of the file? I looked at the tutorial, and it didnt' have anything on eof(). I've been looking around on the net for something, but I'm not having any luck.

    I just want to know why my loop is running infinately and what I need to do to stop it. What error check am I missing?

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    it didnt' have anything on eof()
    That's why you were referred to the FAQ - It's listed as a forum on the main page.

    What error check am I missing?
    You're not checking for success on each of your calls to getline.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. having trouble reading into Struct from File
    By bcianfrocca in forum C++ Programming
    Replies: 9
    Last Post: 09-06-2005, 10:54 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM