C++/Filing

Advertisement


Question
Sir i want to ask a question about filing.
Whenever i do the following i got the output in the form of non-terminating loop.
the code is
myfile.open("shahid.dat",ios::binary|ios::in);
if(!myfile)
{
cerr<<"File was not opened "<<endl;
exit(0);
}
myfile.seekg(0);
myfile.read(reinterpret_cast <char*>(&myemp),sizeof(employee));
while(myfile && !myfile.eof())
{
myemp.printAll();
myfile.read(reinterpret_cast<char *> (&myemp),sizeof(employee));
}
myfile.close();

Please tell me the solution.
I have to submit the assignment in two days.
Thanks

Answer
The problem may be that your C++ facilities do not set eof.  Check the documentation.  I found an alternate way to check for eof.  Please see the following link:
http://www.gidforums.com/t-3801.html

Check for while ( myfile.read(reinterpret_cast <char*>(%26myemp),sizeof(employee)) != 0 ) instead of eof.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Titus B. Ledbetter, Jr.

Expertise

C , MFC, Object Oriented, Artificial Intelligence

Experience

I have over ten years experience in the field of Computer Science, five years experience developing C/C++. I recently wrote a chess program using Object Oriented, C++, MFC.

Education/Credentials
Master's Degree Computer Science from Johns Hopkins

©2016 About.com. All rights reserved.