Thread: file reading error

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    file reading error

    hello,
    i am trying to read from a file until EOF, and print details to the screen.
    it seems to print extra stuff at the end.
    below is my code

    Code:
    list<Things*> i;
    ifstream x ("file.txt");
    
    while(!x.eof())
    {
         Things* temp = new Things;
         x.getline(one);
         x >> two;
         x >> three;
         temp->set_one(one);
         temp->set_two(two);
         temp->set_three(three);
         i.push_back(temp);
    }
    
    ostream& Foo::printAll(ostream& stream) {
      list<Things*>::iterator a;
    
        for(a = i.begin(); a != i.end(); ++a)
            stream << (**a).print_details(stream) << endl;
    
      return stream;
    }
    
    ostream& Bar::print_details(ostream& stream) {
            stream  << "ONe: "        << get_one()           << endl
                    << "Two: "             << get_two()        << endl
                    << "Three: "           << get_three()      << endl
                    << endl;
            return stream;
    }
    and the printout that i actually get when i run my prog is (without the hashes)..
    ###################
    One: eeny
    Two: meeny
    Three: miny

    0x804d1bc
    One:
    Two: meeny
    Three: miny

    0x804d1bc
    ###################

    and my file contains (without the hashes)
    ###################
    eeny
    meeny miny
    ###################


    can anyone see why its printing it twice and those funny address values aswell?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    try a++ instead of ++a

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    also, try no print_details instead of stream << print_details
    Last edited by misplaced; 10-05-2004 at 03:22 AM.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    try a++ instead of ++a
    this did not work

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    also, try no print_details instead of stream << print_details
    ok this stopped the printing of address's,
    it is still printing an extra copy ..like so


    One: eeny
    Two: meeny
    Three: miny

    One:
    Two: meeny
    Three: miny

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    .........???............seems to me like you don't need the for loop either......
    try without it...if that doesn't work post the rest of your code

    what are you trying to do with (**a) ?

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    well i would think i would need the for loop since i want to print out all the nodes in my list. even tho now it only has one, later on i do wish to insert more.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    your code doesn't really make sense to me to begin with.....

    you have a list containing type Things....yet, inside Bar:rint_details() you reference get_one(), get_two() and get_three() as if they were of type Bar.....is this an inherited class?

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I think the problem is here:
    Code:
    while(!x.eof())
    {
    	 Things* temp = new Things;
    	 x.getline(one);
    	 x >> two;
    	 x >> three;
    eof() as a conditional will often cause an "extra" item to be read in. I'm not very good at explaining it, so I recommend you go here:

    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    That reference talks about feof() but the argument holds for eof() as well, as eof() is basically feof() updated for C++ and input streams rather than FILE pointers for C.

    As I understand it, when your code calls x >> three, it terminates input when it sees EOF but it doesn't set EOF indicator yet, so when you go back to the start of the while loop, eof() checks to see if EOF indicator is set. It isn't so eof() still returns false, !x.eof() is still true, and the loop body goes on to try to read some more. The behaviour of getline() and >> is undefined at this point since we have gone beyond the end of the file, and the compiler puts whatever it wants into one, two, and three. It often just doesn't do anything so whatever was in one, two, and three remain there, though you can't count on that. Whatever happens to one, two, and three, the code continues and prints out whatever is in one, two and three, and you end up with an extra set of stuff printed out that you hadn't intended.

    To fix this I'd try changing the conditional to

    while(x.getline(one))

    and droppint the call to getline() within the body of the while loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM