Charlie M
Charlie M

Reputation: 273

File IO Printing to screen

So my teacher gave us the solutions to a lab we had a couple of weeks ago, and it differs from what I was using when I submitted my assignment. I popped this code into my address book program, but nothing prints out, and I was wondering if anyone knew how to get this to print out to the screen from main. I have tried a few things, but the best I can do is to get one entry to print out and nothing else. Heres the code we were given:

void addressBook::showAll2(string fName)
{
 PERSON p;
 PEOPLE2 tp;
 ifstream inData(fName.c_str(), ios::in);
 if(inData.fail())
 throw AddressBookException("Cannot open file to read ");
 people.clear();
 int i = 0;
 while(!inData.eof())
 {
 inData.seekg(i * sizeof(PEOPLE2));
 inData.read(reinterpret_cast<char *>(&tp), sizeof(PEOPLE2));
 p.fName = tp.fName2;
 p.lName = tp.lName2;
 p.Address = tp.Address2;
 people.push_back(p);
 i++;
 }  
 inData.close();
 return; 
}

The only thing I have changed is the throw exception, (our current project). This used to be a bool function, which would return true or false. We were not given the code form main beacuse he isn't concerned with that part, so we rarely ever get it. I'm just wondering if anyone knows how to get this to print out to the screen. I've tried changing it to this:

void addressBook::showAll2(string fName, string &str)
{
 PERSON p;
 PEOPLE2 tp;
 ifstream inData(fName.c_str(), ios::in);
 if(inData.fail())
 throw AddressBookException("Cannot open file to read ");
 people.clear();
 int i = 0;
 while(!inData.eof())
 {
 inData.seekg(i * sizeof(PEOPLE2));
 inData.read(reinterpret_cast<char *>(&tp), sizeof(PEOPLE2));
 p.fName = tp.fName2;
 p.lName = tp.lName2;
 p.Address = tp.Address2;
 people.push_back(p);
 i++;
inData >> str;
 }  
 inData.close();
 return; 
}

and in main I have this:

void printFile()  //prints stored file info to screen
 {  
 string str;
 addressBook *newBook = addressBook::newbookInst();
 Menu *m = Menu::menuInst();
 try
 {
 newBook->showAll2("addressbook", str);

 cout << str << '/n' << endl;
 }
    catch(exception e)
{
    cerr << e.what();
}
 m->waitKey();
 }

So does anyone care to show me what I am missing? The assignment is already done, so feel free to explain it in detail if you'd like, or just post code and I can figure it out. I'm going a little crazy trying to understand how I get this to print with this code. (My version worked just fine btw, but doesn't do any of this.) Thanks

Upvotes: 0

Views: 83

Answers (1)

john
john

Reputation: 87932

Your professors code is wrong because it tests eof in the wrong place, it also does a needless seekg, I'd be worried that your professor is writing bad code.

while(!inData.eof())
{
    inData.seekg(i * sizeof(PEOPLE2));
    inData.read(reinterpret_cast<char *>(&tp), sizeof(PEOPLE2));
    ...
}

should be written like this

for (;;)
{
    inData.read(reinterpret_cast<char *>(&tp), sizeof(PEOPLE2));
    if (inDate.eof())
        break;
    ...
}

You should test for eof after you read not before. It's a very common mistake among newbies, but it's not a mistake your professor should be making. Secondly there's no need for seekg, since you are just going through the file from beginning to end. It's not a bug, but it's not necessary.

inData.close is also unnecessary because the ifstream destructor will close the file for you.

Answer to your question simply is this

inData.read(reinterpret_cast<char *>(&tp), sizeof(PEOPLE2));
p.fName = tp.fName2;
p.lName = tp.lName2;
p.Address = tp.Address2;
cout << p.fName << ' ' << p.lName << ' ' << pAddress << '\n';

Try that code with you professor's version of the loop and you'll probably see why it's bugged.

Upvotes: 1

Related Questions