Loading and Saving Binary Data

J

JoeC

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::eek:fstream f("box.bin", ios::eek:ut | 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::eek:fstream& f){
for(int lp = 0; lp != size; lp++){
f.write((char*)&bitData[lp], sizeof(BYTE));
}
}
 
R

red floyd

JoeC said:
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::eek:fstream f("box.bin", ios::eek:ut | 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::eek:fstream& f){ see comments for load.
for(int lp = 0; lp != size; lp++){
f.write((char*)&bitData[lp], sizeof(BYTE));
}
}
 
J

JoeC

JoeC said:
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::eek:fstream f("box.bin", ios::eek:ut | 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::eek:fstream& 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top