This works:
struct client {
string address;
int toPay;
int id;
};
int main() {
struct client clients[10];
...
file.read( (char*)&clients, sizeof (clients) );
}
What I want to do, is do those things inside of a function.
But how would I have to pass the struct to the function?
If I pass it likes this, read doesn’t work:
void newFunction ( struct client *clients_t) {
...
file.read( (char*)&clients_t, sizeof (clients_t) );
}
This is not going to work, because the
stringdata is not embedded into thestruct. Instead, it has a couple of pointers to the string content. That is whyfile.read( (char*)&clients...)is not going to produce a valid result: thestringwill point to the place where a savedstringonce pointed, but it would no longer represent the data of interest.If you would like to serialize the data like that, embed an entire
chararray in thestruct, with the obvious limitation that there would be a cap on the number of characters and some wasted space.