-4

I wrote a function to retrieve text from a text file and fill out a structure, which will eventually be used to fill out a vector. The text file contains an unknown number of members that I am trying to load. The function works for the first instance. However, the second time the function is called, I get a few lines from the first entry, and then my error checks tells me that it is not reading the second entry correctly.

void FillContact(ifstream &in_stream, Client &TempMarket)
{
    // a bunch of getlines with strings stored in class members of TempMarket
}

int main ()
{
    ifstream in_stream;
    in_stream.open("infile.dat");
    while (!in_stream.eof)
    {
        FillContact(in_stream, TempMarket)
    }
}

The program compiles fine. I think the error lies in attempting to pass by reference to the same function multiple times. Has anyone else encountered such a problem before and how did they fix it? Should I be using pointers instead?

Zong
  • 6,160
  • 5
  • 32
  • 46
SP00N
  • 25
  • 7
  • [MCVE](http://stackoverflow.com/help/mcve) please! – πάντα ῥεῖ Jun 10 '14 at 16:24
  • What is `Client`? Why hide what `FillContact` does? That may be where the problem is, but no one knows since we don't see the code. `The program compiles fine` Just because it "compiles fine" means nothing except you have built an executable. That does not guarantee that the program will run correctly. – PaulMcKenzie Jun 10 '14 at 16:36
  • I bet the problem lies in the FillContact() implementation. You should definitely provide it. Also, what do you mean that the entry is not being read correctly? What happens next? The program crashes? – KjMag Jun 10 '14 at 17:28
  • Arrgh! What I thought was a completely unrelated subfunction was causing me to step through my .dat file. My FillContact function probably works fine, it's probably just that the getline was getting the incorrect line. Thanks for everyone offering their suggestions. I really do appreciate your help. Still very new at this programming stuff and it's more frustrating than fun. For right now at least. I'll send an update of whether or not that's was the case. – SP00N Jun 10 '14 at 18:38

1 Answers1

0

You don't show the body of FillContact, which is probably where your problem is, but an additional problem you have is that you're checking the eof flag, which will only be true after you've read past the end of the file -- see "while( !feof( file ) )” is always wrong, which is equally applicable to C++ though the details may be slightly different.

What you want instead is to have your FillContact function return a flag saying whether it successfully read a contact or not -- if it fails, you should not attempt to use the value of TempMarket as it is not valid. With that, your loop becomes:

while (FillContact(in_stream, TempMarket)) {
    /* do something with TempMarket */

alternately, you can rewrite your FillContact as an overloaded operator>> function and write:

while (in_stream >> TempMarket) {
    /* do something with TempMarket */
Community
  • 1
  • 1
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226