Thread: ifstream only reads 1 item

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    170

    ifstream only reads 1 item

    I am trying to read from a file, but after I read 1 item, I get no more. (eof maybe).

    I am trying to serialize my class (and its subclasses) by overiding the >>/<< and ifstream and ofstream. The ofstream part works fine and my file is built properly. When I read it back in, I only get the first item, and the rest are null,

    Code:
    //AddressBook.cpp overload
    ifstream& operator >>(ifstream &infile, CAddressBook *entry)
    {
      cout << "Loading in a new AddressBook object from the file." << endl;
      infile >> entry->m_contact;
      cout << "Done loading in a new AddressBook object from the file." << endl;
    }
    
    //Contact.cpp overload
    ifstream& operator >>(ifstream &infile, CContact *contact)
    {
      cout << "Reading in Contact info." << endl;
      infile >> contact->m_fname;
      cout << "I read first name as :" << contact->m_fname << endl;
      infile >> contact->m_lname;
      cout << "I read last name as :" << contact->m_lname << endl;
      infile >> contact->m_pnumber;
      infile >> contact->m_email;
      cout << "I read email as :" << contact->m_email << endl;
      cout << "Done reading in Contact info." << endl;
    }
    
    //PhoneNum.cpp overload
    ifstream& operator >>(ifstream &infile, CPhoneNum *pnum)
    {
      cout << "Reading in the phonenumber." << endl;
      infile >> pnum->m_areacode;
      cout << "Read areacode as:" << pnum->m_areacode << endl;
      infile >> pnum->m_prefix;
      cout << "Read prefix as:" << pnum->m_prefix << endl;
      infile >> pnum->m_suffix;
      cout << "Read suffix as:" << pnum->m_suffix << endl;
      cout << "Done reading in the phonenumber." << endl;
    }
    All I get is the first name, all other fields are NULL.

    I added all the obnoxios couts to try and debug what was happening. Any ideas?
    Best Regards,

    Bonkey

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    What does your stream insertion (<<) operator look like? What does a saved file of this class look like?

    Is it possible your file isn't being created with spaces or newlines between the values?

    Also, why do you overload the operators only for ifstream?

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    What does your stream insertion (<<) operator look like?
    infile >> myAddressBook;

    What does a saved file of this class look like?

    [email protected]

    with nulls that didnt show.
    George\0Bush\0999\0888\07777\[email protected] \0

    Is it possible your file isn't being created with spaces or newlines between the values?
    I just used nulls. I assumed they would work. It does read the first name correctly. It does not read the whole thing into first name.

    Also, why do you overload the operators only for ifstream?


    I don't just not posting everything. If you need to see more, I can post it.

    edit: typod ifstream instead of infile.
    Last edited by bonkey; 11-04-2002 at 02:57 PM.
    Best Regards,

    Bonkey

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    I changed to white space and that seems to have fixed it. Thanks!!!
    Best Regards,

    Bonkey

  5. #5
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Yeah, that'd be it. The stream extraction operator (>>) only delimits at space characters, single spaces, newlines, and tabs. If you wanted to use something else you would have to get the data directly and tokenize it in your program, but using a valid delimiter is usaully simpler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 12:52 PM
  2. Retail Outlet Managment System - the 4th
    By Presidentofusa in forum C Programming
    Replies: 3
    Last Post: 11-10-2007, 10:44 PM
  3. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM