Trying to read from a file into an array

...hey guys (and gals?). Nube working on the Serendipity project in the Gaddis book. I'm at the point of trying to read data from a file into an array. Heres a the chunk of code that is giving me the error:

BookData myData[SIZE];
int count = 0;


int main()
{
fstream data;
data.open("bookData.dat", ios::in|ios::binary);
data.read(reinterpret_cast<char*>(myData), sizeof(myData));
data.close();

...and heres the error... _BLOCK_TYPE_IS_VALID(phead->nBlockUse)

I did some searches online, and it looks like I'm probably trying to write to an unallocated area of memory, so I think the problem is probably with my data.read line. Here is what my structure looks like:

const int SIZE = 20;
struct BookData
{
string bookTitle;
string isbn;
string author;
string publisher;
string dateAdded;
int qtyOnHand;
double wholesale;
double retail;
};

...any help is appreciated...thanx...
When you save and read an std::string like that, you are not just saving the string data, you are also saving other stuff like the buffer and stuff you don't want. I would save/load it in normal mode and use either >> or std::getline() to get the data.
...thanx for the reply...I was thinking the same thing about using the binary for reading and writing...not sure why I am using that other than thats what the instructor is suggesting, but as long as I accomplish the task, it doesn't matter how I do it.

...I'll keep at it and post more questions...
...I think the problem here is that I have strings in my structure, and since the length is unknown, I having problems when I read to/from a file. Going to try it with char instead of string and see if that helps...
Topic archived. No new replies allowed.