Difficulties upon reading binary file

Hi all! The file below, compiles well but does not work.The binary file, records.bin is there. With a cat I saw it. The trasfered file in memory, is there , but I don't how to work with. "records.bin" contains two integers and one string. May somebody help me !
(CODE]#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct PlayerRecord {
int age;
int score;
string name;
};

int main ()
{

streampos size;
char * memblock;

ifstream file ("records.bin", ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();

cout << "the entire file content is in memory\n";

delete[] memblock;
}
else cout << "Unable to open file";

cout << "WORKING DIRECTLY ON THE BINARY REGISTERD FILE(records.bin)\n";

PlayerRecord in_rec;
int x=3;//for an integer, says my book
file.read( reinterpret_cast<char*>( & x ), sizeof( x ));

cout << "age: "<<in_rec.age << endl;// shows a wrong number

cout << "idem for score\n";
cout << "How to work with the entire file content in memory ??\n";

}

~
35,68-75 Bot
[/CODE]
There are a number of problems in the design.
1
2
3
4
5
struct PlayerRecord {
    int age;
    int score;
    string name;
};

It isn't possible to read/write a struct like this directly to/from a binary file because it contains a complex type std::string. The string contains its own internal pointers to the actual character data which is stored in some other block of memory. Such pointers will no longer be valid when later reading from the file - that memory will be re-used for some other purpose.

Possible solutions:
1. change from a std::string to a plain character array.
OR
2. use customised code to first write the string length, then the character data - reading is similar, first read the length, then the character data.

In the above program, the file is read into a block of memory. Then the memory is deleted and the file is closed, meaning neither the memory data nor the file is accessible afterwards.

Here, the file is closed at the time of the file access. There is also no check on the file status after the read, to check whether or not the file access was successful:
 
file.read( reinterpret_cast<char*>( & x ), sizeof( x ));


Thank you veroky much for your reply. I think this program is above my forces. So, forget it.
The book of Alex Allain, page 434, gives the binary.cpp source code, which produces the object code, and also the registered binary. In the same code he wrires and reads. It works well.
I'm not familiar with that book by Alex Allain, though assume his code covers some of the things I mentioned.

If you think this is too hard for now, You could try working with a binary file which contains just data like this, and see how you get on:
1
2
3
4
struct PlayerRecord {
    int age;
    int score;
};

Forget about the string for now. You should find this easier to work with, just for learning / practice purposes.

Also, as things get more complex, rather than trying to do all the work inside main(), you could use separate functions to break up the code into smaller pieces, for example one function to write a single record to the file, and another function to read a single record from the file.
Topic archived. No new replies allowed.