473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Understanding binary files.

I am writing a program that I am trying to learn and save binary
files. This is the page I found as a source:
http://www.angelfire.com/country/ald...aryFileIO.html

I have successfully created and used txt files.

I am trying to save then load in an array of pointers to objects:

class Map{

int xlen;
int ylen;

int mapTotal;

HWND hwnd;

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);
}
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.

Mar 28 '07 #1
15 2962
JoeC wrote:
I am writing a program that I am trying to learn and save binary
files. This is the page I found as a source:
http://www.angelfire.com/country/ald...aryFileIO.html

I have successfully created and used txt files.

I am trying to save then load in an array of pointers to objects:

[..]
void Map::load(std::ifstream& f){

f.seekg (0);
f.read((char*)&mapTotal, sizeof(int));
Strange assymetry. You write using 'sizeof(mapTotal)', but you read
using 'sizeof(int)'. Why?
f.read((char*)&playArea, sizeof(space) * mapTotal);
Another strange assymetry (and probably a logical error). You read
'mapTotal' spaces, buy you only wrote one (see below).
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));
If there are 'mapTotal' spaces in your object, why do you only write
one?
>
MessageBox(NULL, "Saving...", "Simulation", MB_OK);
}
[..]

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.
And just for the curiousity's sake, did you have a question?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 28 '07 #2

"JoeC" <en*****@yahoo.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...
>I am writing a program that I am trying to learn and save binary
files. This is the page I found as a source:
http://www.angelfire.com/country/ald...aryFileIO.html

I have successfully created and used txt files.

I am trying to save then load in an array of pointers to objects:

class Map{

int xlen;
int ylen;

int mapTotal;

HWND hwnd;

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);
}
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.

Are you running into a particular problem? Or are you looking to have
someone bug-check all your code? Or are you asking how to verify it saved?
It the latter, then load it back up and compare the loaded data with the
data before the save...Not sure what your exact question is.
Mar 28 '07 #3
On Mar 28, 10:34 am, "JoeC" <enki...@yahoo.comwrote:
I am writing a program that I am trying to learn and save binary
files. This is the page I found as a source:http://www.angelfire.com/country/ald...aryFileIO.html
<snip>
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.
I will assume the question is "how do I make sure this is working
correctly?"

Basically, you should figure out what results you are expecting, and
compare those to the actual.

For example, let's say you have an int that is 4 bytes long, with a
value of 0x01020304. If you do a binary dump of the file (e.g., with
the 'od' program under UNIX or loading it into an appropriate binary
viewer in Visual Studio or whatever), you would expect to see the
bytes 01, 02, 03, 04, or possibly 04, 03, 02, 01.

I would recommend creating a small test case with some goofy values
(e.g., 01020304) and confirming, by hand, that it's storing what you
think. I'd be especially careful with the pointer part, as it is
known to be tricky to serialize pointers correctly.

And FYI, there will be some people on this group who tell you that
binary output is the work of the devil, and that text is the
appropriate medium for all exchanges. Since, as I understand it, your
goal is to learn how to do binary output, ignore them.

Michael

Mar 28 '07 #4
On Mar 28, 10:34 am, "JoeC" <enki...@yahoo.comwrote:
I am writing a program that I am trying to learn and save binary
files. This is the page I found as a source:http://www.angelfire.com/country/ald...aryFileIO.html
<snip>
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.
I will assume the question is "how do I make sure this is working
correctly?"

Basically, you should figure out what results you are expecting, and
compare those to the actual.

For example, let's say you have an int that is 4 bytes long, with a
value of 0x01020304. If you do a binary dump of the file (e.g., with
the 'od' program under UNIX or loading it into an appropriate binary
viewer in Visual Studio or whatever), you would expect to see the
bytes 01, 02, 03, 04, or possibly 04, 03, 02, 01.

I would recommend creating a small test case with some goofy values
(e.g., 01020304) and confirming, by hand, that it's storing what you
think. I'd be especially careful with the pointer part, as it is
known to be tricky to serialize pointers correctly.

And FYI, there will be some people on this group who tell you that
binary output is the work of the devil, and that text is the
appropriate medium for all exchanges. Since, as I understand it, your
goal is to learn how to do binary output, ignore them.

Michael

Mar 28 '07 #5
On Mar 28, 7:34 pm, "JoeC" <enki...@yahoo.comwrote:
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...aryFileIO.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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Mar 29 '07 #6
On Mar 28, 12:57 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
JoeC wrote:
I am writing a program that I am trying to learn and save binary
files. This is the page I found as a source:
http://www.angelfire.com/country/ald...aryFileIO.html
I have successfully created and used txt files.
I am trying to save then load in an array of pointers to objects:
[..]
void Map::load(std::ifstream& f){
f.seekg (0);
f.read((char*)&mapTotal, sizeof(int));

Strange assymetry. You write using 'sizeof(mapTotal)', but you read
using 'sizeof(int)'. Why?
f.read((char*)&playArea, sizeof(space) * mapTotal);

Another strange assymetry (and probably a logical error). You read
'mapTotal' spaces, buy you only wrote one (see below).
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));

If there are 'mapTotal' spaces in your object, why do you only write
one?
MessageBox(NULL, "Saving...", "Simulation", MB_OK);
}
[..]
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.

And just for the curiousity's sake, did you have a question?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks, I will look at what you wrote and make some corrections. I
realized that I have pointers in the spaces so each space will have to
save themselves. I still have some work to do, I don't really
understand how to save binary files. I know how to do text files and
I am just trying to save data in a bin file.

Mar 30 '07 #7
On Mar 28, 2:27 pm, "Christopher Pisz" <some...@somewhere.netwrote:
"JoeC" <enki...@yahoo.comwrote in message

news:11**********************@n76g2000hsh.googlegr oups.com...
I am writing a program that I am trying to learn and save binary
files. This is the page I found as a source:
http://www.angelfire.com/country/ald...aryFileIO.html
I have successfully created and used txt files.
I am trying to save then load in an array of pointers to objects:
class Map{
int xlen;
int ylen;
int mapTotal;
HWND hwnd;
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);
}
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.

Are you running into a particular problem? Or are you looking to have
someone bug-check all your code? Or are you asking how to verify it saved?
It the latter, then load it back up and compare the loaded data with the
data before the save...Not sure what your exact question is.
First, am I doing it right, then how do I know of it saved?

Mar 30 '07 #8
On Mar 28, 4:14 pm, "Michael" <mchlg...@aol.comwrote:
On Mar 28, 10:34 am, "JoeC" <enki...@yahoo.comwrote:I am writing a program that I am trying to learn and save binary
files. This is the page I found as a source:http://www.angelfire.com/country/ald...aryFileIO.html

<snip>
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.

I will assume the question is "how do I make sure this is working
correctly?"

Basically, you should figure out what results you are expecting, and
compare those to the actual.

For example, let's say you have an int that is 4 bytes long, with a
value of 0x01020304. If you do a binary dump of the file (e.g., with
the 'od' program under UNIX or loading it into an appropriate binary
viewer in Visual Studio or whatever), you would expect to see the
bytes 01, 02, 03, 04, or possibly 04, 03, 02, 01.

I would recommend creating a small test case with some goofy values
(e.g., 01020304) and confirming, by hand, that it's storing what you
think. I'd be especially careful with the pointer part, as it is
known to be tricky to serialize pointers correctly.

And FYI, there will be some people on this group who tell you that
binary output is the work of the devil, and that text is the
appropriate medium for all exchanges. Since, as I understand it, your
goal is to learn how to do binary output, ignore them.

Michael

I assume that I have to save the piece of data then load it back up
the same way I saved it. I have been working on my code and trying to
save my data correctly.

Mar 30 '07 #9
On Mar 29, 7:18 am, "James Kanze" <james.ka...@gmail.comwrote:
On Mar 28, 7:34 pm, "JoeC" <enki...@yahoo.comwrote:
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...aryFileIO.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.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Thanks, I have worked with text files and got them to work. I realize
that I am learning to do binary files on a complex program and I
should get it to work and understand what I am doing with a simpler
format. It might be too dificult and above my sill to save the
program I am doing in a file.

Mar 30 '07 #10
JoeC wrote:
On Mar 28, 2:27 pm, "Christopher Pisz" <some...@somewhere.netwrote:
>"JoeC" <enki...@yahoo.comwrote in message
[..]
Are you running into a particular problem? Or are you looking to have
someone bug-check all your code? Or are you asking how to verify it
saved? It the latter, then load it back up and compare the loaded
data with the data before the save...Not sure what your exact
question is.

First, am I doing it right, then how do I know of it saved?
Unless you have a different program that can read what you saved and
confirm, a round-trip is usually a way to go. Read back what you saved
in another run and see if the information is valid.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 30 '07 #11
On Mar 30, 8:08 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
JoeC wrote:
On Mar 28, 2:27 pm, "Christopher Pisz" <some...@somewhere.netwrote:
"JoeC" <enki...@yahoo.comwrote in message
[..]
Are you running into a particular problem? Or are you looking to have
someone bug-check all your code? Or are you asking how to verify it
saved? It the latter, then load it back up and compare the loaded
data with the data before the save...Not sure what your exact
question is.
First, am I doing it right, then how do I know of it saved?

Unless you have a different program that can read what you saved and
confirm, a round-trip is usually a way to go. Read back what you saved
in another run and see if the information is valid.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
I created another program in which I was able to save and read binary
data. I also worked on the program I am trying to create and it
doesn't work. But I will still if I can find the problems myself.

Apr 1 '07 #12
JoeC wrote:
On Mar 30, 8:08 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>JoeC wrote:
>>On Mar 28, 2:27 pm, "Christopher Pisz" <some...@somewhere.net>
wrote:
"JoeC" <enki...@yahoo.comwrote in message
[..]
Are you running into a particular problem? Or are you looking to
have someone bug-check all your code? Or are you asking how to
verify it saved? It the latter, then load it back up and compare
the loaded data with the data before the save...Not sure what your
exact question is.
>>First, am I doing it right, then how do I know of it saved?

Unless you have a different program that can read what you saved and
confirm, a round-trip is usually a way to go. Read back what you
saved in another run and see if the information is valid.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

I created another program in which I was able to save and read binary
data. I also worked on the program I am trying to create and it
doesn't work. But I will still if I can find the problems myself.
By the other program I meant not written by you. Example, if you are
to write an raster image file, like a JFIF or a PNG, you can pretty
much use any viewer to see if what you wrote can be read in. Or, in
case of a generic binary file, you could use a hex editor to see what
bytes you wrote look like and compare them with your specification.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 2 '07 #13
On Apr 1, 8:14 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
JoeC wrote:
On Mar 30, 8:08 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
JoeC wrote:
On Mar 28, 2:27 pm, "Christopher Pisz" <some...@somewhere.net>
wrote:
"JoeC" <enki...@yahoo.comwrote in message
[..]
Are you running into a particular problem? Or are you looking to
have someone bug-check all your code? Or are you asking how to
verify it saved? It the latter, then load it back up and compare
the loaded data with the data before the save...Not sure what your
exact question is.
>First, am I doing it right, then how do I know of it saved?
Unless you have a different program that can read what you saved and
confirm, a round-trip is usually a way to go. Read back what you
saved in another run and see if the information is valid.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
I created another program in which I was able to save and read binary
data. I also worked on the program I am trying to create and it
doesn't work. But I will still if I can find the problems myself.

By the other program I meant not written by you. Example, if you are
to write an raster image file, like a JFIF or a PNG, you can pretty
much use any viewer to see if what you wrote can be read in. Or, in
case of a generic binary file, you could use a hex editor to see what
bytes you wrote look like and compare them with your specification.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
I am getting close. I can save many parts of my map but the graphics
are the challenge. I got this to work:

void space::load(std::ifstream& f){
int s = colors.size();
bool t;
woods = 0;
gr->load(f);
f.read((char*)&loc, sizeof(coord));
f.read((char*)&quality,sizeof(int));
f.read((char*)&terrain,sizeof(int));
f.read((char*)&s, sizeof(int));
for(int lp = 0; lp != s; lp++){
f.read((char*)&colors[lp], sizeof(DWORD));
}
f.read((char*)&t, sizeof(bool));
//if(t){f.read((char*)&woods, sizeof(forrest));}
}

void space::save(std::ofstream& f){
bool t;
int size = colors.size();
gr->save(f);
f.write((char*)&loc, sizeof(coord));
f.write((char*)&quality,sizeof(int));
f.write((char*)&terrain,sizeof(int));
f.write((char*)&size, sizeof(int));
for(int lp = 0; lp != colors.size(); lp++){
f.write((char*)&colors[lp], sizeof(DWORD));
}
if(woods){t=true;} else {t=false;}
f.write((char*)&t, sizeof(bool));
//if(t){f.write((char*)woods, sizeof (forrest));}
}

But I am havign trouble loading in the objects that have graphics like
gr which is a binary graphic that I crated and woods which holds a
bitmap graphic. Here is what I have for loading the binary gr
graphic:

void graphics::load(std::ifstream& f){
bitData.clear();
for(int lp = 0; lp != len; lp++){
f.read((char*)&bitData[lp],sizeof(BYTE));
}
}

void graphics::save(std::ofstream& f){
for(int lp = 0; lp != len; lp++){
f.write((char*)&bitData[lp],sizeof(BYTE));
}
}

What I get is the current terrain graphics on a saved map.

Apr 2 '07 #14
JoeC wrote:
[..] I am havign trouble loading in the objects that have graphics like
gr which is a binary graphic that I crated and woods which holds a
bitmap graphic. Here is what I have for loading the binary gr
graphic:

void graphics::load(std::ifstream& f){
bitData.clear();
Since I have no idea what 'bitData' is, I can't say whether calling
'clear' is the right thing to do here. It is suspect, though. If
'bitData' is a standard container, 'clear' removes all elements, and
then it has no elements. Perhaps you need to pre-allocate them.
for(int lp = 0; lp != len; lp++){
f.read((char*)&bitData[lp],sizeof(BYTE));
The use of the indexing operator suggests that either 'bitData' is
a vector (which would mean undefined behaviour since it contains no
elements after 'clear') or a map. If the latter, you might be OK,
but who knows...
}
}

void graphics::save(std::ofstream& f){
for(int lp = 0; lp != len; lp++){
f.write((char*)&bitData[lp],sizeof(BYTE));
}
}

What I get is the current terrain graphics on a saved map.
Which would probably indicate that you don't read anything. So,
check the functionality of your 'load' routine carefully.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 3 '07 #15
On Apr 3, 8:22 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
JoeC wrote:
[..] I am havign trouble loading in the objects that have graphics like
gr which is a binary graphic that I crated and woods which holds a
bitmap graphic. Here is what I have for loading the binary gr
graphic:
void graphics::load(std::ifstream& f){
bitData.clear();

Since I have no idea what 'bitData' is, I can't say whether calling
'clear' is the right thing to do here. It is suspect, though. If
'bitData' is a standard container, 'clear' removes all elements, and
then it has no elements. Perhaps you need to pre-allocate them.
Sorry Bit data is a vector of BYTE type.
for(int lp = 0; lp != len; lp++){
f.read((char*)&bitData[lp],sizeof(BYTE));

The use of the indexing operator suggests that either 'bitData' is
a vector (which would mean undefined behaviour since it contains no
elements after 'clear') or a map. If the latter, you might be OK,
but who knows...
}
}
void graphics::save(std::ofstream& f){
for(int lp = 0; lp != len; lp++){
f.write((char*)&bitData[lp],sizeof(BYTE));
}
}
What I get is the current terrain graphics on a saved map.

Which would probably indicate that you don't read anything. So,
check the functionality of your 'load' routine carefully.
That is my load function. It is possible that I am not loading
anything because the old graphics are still there after I load in the
map. I get the new map with the old graphic symbols that is the
graphics that were created when I created the map from the current
execution stay although I loaded in saved data.


Apr 3 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there...
28
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
9
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the...
8
by: dagecko | last post by:
Hi I would like to know how to detect if a file is binary or not. It's important for me but I don't know where to start. Ty
10
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in...
68
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
3
by: masood.iqbal | last post by:
Hi, Kindly excuse my novice question. In all the literature on ifstream that I have seen, nowhere have I read what happens if you try to read a binary file using the ">>" operator. I ran into...
9
by: deepakvsoni | last post by:
are binary files portable?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.