Re: Understanding binary files.
On Mar 28, 7:34 pm, "JoeC" <enki...@yahoo.com> wrote:
I am writing a program that I am trying to learn and save binary
files.
In what format?
All data has some format. Binary is not, in itself, a format;
there are many different binary formats (e.g. XDR, BER, etc.).
Until you specify the format, you can't really talk about how to
do IO.
This is the page I found as a source:http://www.angelfire.com/country/ald=
ev0/cpphowto/cpp_BinaryFileIO.html
A quick check shows the page to present a curious mishmash of
C++ and Posix, without specifying which is which. And the C++
seems out of date (e.g. <fstream.h>), although not much has
changed at the level the page addresses. I'm not sure I'd
recommend it (although I've definitly seen a lot worse).
I have successfully created and used txt files.
I am trying to save then load in an array of pointers to objects:
An array of pointers? Generally speaking, there is no possible
way to format a pointer so that it can be reread successfully.
Depending on what the pointer is being used for, you either have
to replace it with some other type of identifier, or format what
it points to. (In your case, doubtlessly the latter.)
class Map{
int xlen;
int ylen;
int mapTotal;
HWND hwnd;
Curious as to what HWND might be. (Not that it really matters.)
space * playArea;
grTerrain grTerr;
...
Map::Map(HWND h, const int x, const int y){
xlen = x;
ylen = y;
hwnd = h;
playArea = new space[(xlen * ylen)];
create();
}
void Map::load(std::ifstream& f){
f.seekg (0);
f.read((char*)&mapTotal, sizeof(int));
f.read((char*)&playArea, sizeof(space) * mapTotal);
MessageBox(NULL, "Loading...", "Simulation", MB_OK);
}
void Map::save(std::ofstream& f){
f.seekp (0);
f.write((char*)&mapTotal, sizeof(mapTotal));
f.write((char*)&playArea, sizeof(space));
MessageBox(NULL, "Saving...", "Simulation", MB_OK);
}
Two immediate problems:
-- You haven't defined a format, so you're getting some random
format decided by the compiler. As long as you reread with
exactly the same executable (on the same machine, compiled
with the same compiler, same version and with the same
options), reading and writing basic types (e.g. int, etc.)
will probably work, but that's about it. For anything else,
you need to define a format, convert to it on output, and
from it on input.
-- You're trying to write and read a pointer. That is simply
impossible. You must read and write what it points to.
Note too that the need for casts here should make you very, very
suspicious that you're doing something wrong. (But it's not an
absolute---for various reasons, I generally format binary into
a buffer of unsigned char, but still use basic_[io]stream<char>,
rather than basic_[io]stream<unsigned char>, so I usually need
the cast as well.)
The real problem is that you haven't even thought about the
format. What do you actually want in the file. Described down
to the last byte. (Note that this first step is really no
different than what you'd do for text IO. The differences come
later, because there is a good deal of support for implementing
text formats, i.e. the << and >> operators, and none for
binary.)
case ID_LOAD: {
std::ifstream f("data.bin", ios::in | ios::binary);
board->load(f);
f.close();
InvalidateRect(hwnd,NULL,FALSE);
break;
}
case ID_SAVE:{
std::ofstream f("data.bin", ios::out | ios::binary);
board->save(f);
f.close();
break;
}
I posted the parts of the program that apply to the saving and
loading. The program does save and creates a bin files 53kb so it
seems like the data is saving but unlike txt files, it is hard to
verify if the informations saved correctly.
That's one reason why text files are generally preferred. It's
a lot easier to debug a text file, since you can look at it with
just about any editor. Another reason to prefer text files is
that a lot of the work has already been done for you. On the
other hand, formatting a binary file generally takes less CPU
(not usually an issue, but sometimes), and depending on the
data, a binary file might be smaller (but I've also seen cases
of the opposite); a text file can generally be compressed to
make it smaller than a binary file, but then reading and writing
it take a lot more CPU.
Anyway, the first thing to do is to sit down and make a sort of
a schema as to what you want in the file, where. Precisely, not
just something vague.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34