I'm writing just a simple program that gets input from user and stores them in a file and displays the contents when required. Following is my program.
What is eating my head is, why it displays the last record two times? Tried everything, might be due to my limited knowledge, Please help me!
while(!data.eof())
Only kicks you out of the while loop after you've failed to read from the file, so the contents you display stay the same as from the previous entry.
while(data.read((char*) &e, sizeof(e)))
Or you can calculate the numbers or registers and use a for.
Also you've got a little problem with your structure: string name, address;. The idea with binary files is constant size of registers, so you can get the one you want in constant time.
And I think the string only contents a pointer to where is the data stored, so if you do data.write((char*) & some_string, sizeof(string));
you'll get the direction and not the words.
You can't just read and write your employee records in binary the way you do because they contain std::string objects which use pointers. You will write the pointer but not the string itself.