write and read a vector from file

2 posts / 0 new
Last post
emeck
emeck's picture
Offline
Last seen: 11 years 9 months ago
Joined: 2011-01-15 15:22
write and read a vector from file

Hi guys,

I'm trying to write to file a vector of classes and then reading it from file, but I don't get it working. The code compiles and the program creates the file, but with what seems garbage (I'm not writing in binary mode). This is the code:

  1. foutBuddiesList.write(reinterpret_cast<char *>(&(buddiesList)), buddiesList.size() * sizeof(cBuddy));

cBuddy is the class and buddiesList is the vector containing the data. What am I doing wrong? Do I have to do a loop to append each vector member at a time?

Thanks for the help.

hypex
hypex's picture
Offline
Last seen: 3 months 2 weeks ago
Joined: 2011-09-09 16:20
Figure this out? Does it

Figure this out? Does it matter that you have &(buddiesList) instead of &buddiesList?

If your buddiesList is a class then wouldn't that write the internal class structure to disk? Where is the member containing the vectors? I thought you would need to store the vector pointer in the class and write that pointer to disk.

Log in or register to post comments