I've been toying around with File I/O in C++ for about a day now, and cannot quite figure out how to write a double pointer (It's for a Multidimensional Array) to a file. Everytime I try to write it, it just writes garbage, which leads me to believe that 1) I'm doing something wrong, and 2) I am writing data to a file I don't have access too (oops).
My guess is, I am not converting my Array to a (char *) correctly. I'm not quite sure how you would do that in the first place as I've never worked with double pointers before.
You say that it "just writes garbage". But what are you expecting the contents of your array to look like in a file?
Furthermore you are writing your array as if it was one contiguous lump of memory. But this is not how you allocated it:
1 2 3 4 5 6 7
cFoo **Bar = new cFoo*[ARRAY_WIDTH];
// Setup Array
for(i = 0; i < ARRAY_WIDTH; i++)
{
Bar[i] = new cFoo[ARRAY_HEIGHT];
}
You have one lump of memory to contain pointers, each one pointing to a separate lump of memory. Your array is likely to be dotted all around your memory. It is not likely to all be in a neat contiguous lump!
Personally I would consider writing out the textual representation of the values a bit like this:
1 2 3
for(size_t x = 0; x < ARRAY_WIDTH; ++x)
for(size_t y = 0; y < ARRAY_HEIGHT; ++y)
fsFile << inBar[x][y] << '\n';
The file should look like a series of NULs and STX/SOH in notepad++ (As I know what the data looks like written to a file), which is what I use to see if what I wrote to it worked correctly.
And, oh. I hadn't realized that's what I was doing. Oops :P I knew the thing about memory, I just thinking too hard, thank you for pointing that out to me! :) If anyone is interested in how I solved this, this is the changed code:
BTW A textual representation would be valid, but a dramatic waste of space when the integers get to values like 31722. I would much prefer a Binary approach, sure if the space was maybe one or two digits that would be good, but at that point I'd probably opt for using bytes/chars for file size reasons.