Loading and Saving Binary Data

Discussion in 'C++' started by JoeC, Nov 20, 2007.

  1. JoeC

    JoeC Guest

    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));
    }
    }
     
    JoeC, Nov 20, 2007
    #1
    1. Advertising

  2. JoeC

    red floyd Guest

    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::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));
    > }
    > }
     
    red floyd, Nov 20, 2007
    #2
    1. Advertising

  3. JoeC

    JoeC Guest

    On Nov 19, 10:41 pm, red floyd <> wrote:
    > 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::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.
     
    JoeC, Nov 20, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Kandell
    Replies:
    4
    Views:
    4,167
    eeebop
    Dec 10, 2004
  2. Shahid Juma

    Loading/Saving Data

    Shahid Juma, Mar 29, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    329
    Shahid Juma
    Mar 29, 2006
  3. Jerivix Entadi
    Replies:
    4
    Views:
    381
    John Harrison
    Feb 23, 2004
  4. Replies:
    0
    Views:
    482
  5. Sune

    Saving binary data in MySQL

    Sune, Nov 4, 2005, in forum: ASP General
    Replies:
    2
    Views:
    167
Loading...

Share This Page