468,512 Members | 1,423 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Loading and Saving Binary Data

I have successfully saved and re-loaded binary data but I am having
some trouble with some data:

I wrote a binary graphics object and it takes data in a BYTE form.
Here is the data I have and how I save then load it:

static BYTE bits [] =
{0xff,0xff, 0x00,0x00, 0x7f,0xfe, 0x7f,0xfe,
0x7f,0xfe, 0x7f,0xfe, 0x7f,0xfe, 0x7e,0x7e,
0x7e,0x7e, 0x7f,0xfe, 0x7f,0xfe, 0x7f,0xfe,
0x7f,0xfe, 0x7f,0xfe, 0x7f,0xfe, 0x00,0x00};

graphics gr(bits);

std::ofstream f("box.bin", ios::out | ios::binary);
gr.save(f);
f.close();

std::ifstream f("box.bin", ios::in | ios::binary);
gr.load(f);
f.close();
void graphics::load(std::ifstream& f){
bitData.clear();
for(int lp = 0; lp != size; lp++){
f.read((char*)&bitData[lp],sizeof(BYTE));
}
}

void graphics::save(std::ofstream& f){
for(int lp = 0; lp != size; lp++){
f.write((char*)&bitData[lp], sizeof(BYTE));
}
}
Nov 20 '07 #1
2 3451
JoeC wrote:
I have successfully saved and re-loaded binary data but I am having
some trouble with some data:
That's nice. Would you mind being a bit more specific? What trouble
are you having? What are you expecting?

Post a minimal *COMPILABLE* example that exhibits the behavior in
question. Note that your fragment below is not a compilable example.
I wrote a binary graphics object and it takes data in a BYTE form.
Here is the data I have and how I save then load it:
BYTE undefined.
static BYTE bits [] =
{0xff,0xff, 0x00,0x00, 0x7f,0xfe, 0x7f,0xfe,
0x7f,0xfe, 0x7f,0xfe, 0x7f,0xfe, 0x7e,0x7e,
0x7e,0x7e, 0x7f,0xfe, 0x7f,0xfe, 0x7f,0xfe,
0x7f,0xfe, 0x7f,0xfe, 0x7f,0xfe, 0x00,0x00};
graphics undefined.
graphics gr(bits);

std::ofstream f("box.bin", ios::out | ios::binary);
gr.save(f);
f.close();

std::ifstream f("box.bin", ios::in | ios::binary);
gr.load(f);
f.close();
void graphics::load(std::ifstream& f){
bitData and size undefined.
bitData.clear();
for(int lp = 0; lp != size; lp++){
f.read((char*)&bitData[lp],sizeof(BYTE));
no error checking on istream::read().
}
}
void graphics::save(std::ofstream& f){
see comments for load.
for(int lp = 0; lp != size; lp++){
f.write((char*)&bitData[lp], sizeof(BYTE));
}
}
Nov 20 '07 #2
On Nov 19, 10:41 pm, red floyd <no.s...@here.dudewrote:
JoeC wrote:
I have successfully saved and re-loaded binary data but I am having
some trouble with some data:

That's nice. Would you mind being a bit more specific? What trouble
are you having? What are you expecting?

Post a minimal *COMPILABLE* example that exhibits the behavior in
question. Note that your fragment below is not a compilable example.
I wrote a binary graphics object and it takes data in a BYTE form.
Here is the data I have and how I save then load it:

BYTE undefined.
static BYTE bits [] =
{0xff,0xff, 0x00,0x00, 0x7f,0xfe, 0x7f,0xfe,
0x7f,0xfe, 0x7f,0xfe, 0x7f,0xfe, 0x7e,0x7e,
0x7e,0x7e, 0x7f,0xfe, 0x7f,0xfe, 0x7f,0xfe,
0x7f,0xfe, 0x7f,0xfe, 0x7f,0xfe, 0x00,0x00};

graphics undefined.
graphics gr(bits);
std::ofstream f("box.bin", ios::out | ios::binary);
gr.save(f);
f.close();
std::ifstream f("box.bin", ios::in | ios::binary);
gr.load(f);
f.close();
void graphics::load(std::ifstream& f){

bitData and size undefined.
bitData.clear();
for(int lp = 0; lp != size; lp++){
f.read((char*)&bitData[lp],sizeof(BYTE));

no error checking on istream::read().
}
}
void graphics::save(std::ofstream& f){

see comments for load.
for(int lp = 0; lp != size; lp++){
f.write((char*)&bitData[lp], sizeof(BYTE));
}
}


The program compiles and runs. It seems to save data, it create the
file and seems to be about the right size but I can't seem to get the
data to load. That is I save the graphics then when I load it it does
not display the saved shape. This is the only relieve5t cone and it
does not seem to be wrong it just doesn't load.
Nov 20 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by rixdelei | last post: by
4 posts views Thread by Jerivix Entadi | last post: by
reply views Thread by Tommy Christian | last post: by
4 posts views Thread by adamrobillard | last post: by
10 posts views Thread by eholz1 | last post: by
5 posts views Thread by Chuck Anderson | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.