Results 1 to 3 of 3

Thread: How to create ptr to ifstream and use structures?[RESOLVED]

Threaded View

  1. #1

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157

    How to create ptr to ifstream and use structures?[RESOLVED]

    Hi,
    I wrote a C++ piece of code to read a binary header (for a bitmap). It works well. I can load the image into an array. No problem!

    At the bottom of this posting, you see how I created the object 'in' and read some binary data into 'h' which is a structure (abbreviated piece of code).

    1. How would I create this object 'in' using new, then read the values into 'h'? Something like this?

    ifstream *p = new ifstream("stone1.bmp",ios::binary | ios::in);

    How to read data into object???

    2. I can read the binary data into the structure, one member at a time. How would I read the data into the structure 'h' in one line?



    Thanks,
    ChuckB

    Code:
      typedef struct {
        unsigned short type;
        unsigned long size;
        unsigned short reserved1;
        unsigned short reserved2;
        unsigned long offset;  
      } HEADER;
      HEADER h;  
    
      ifstream in("stone1.bmp",ios::binary | ios::in); 
      if(!in)exit(EXIT_FAILURE);
    
      in.read((char*)&h.type, sizeof h.type);
      cout << "File type: " << h.type << endl;
      in.read((char*)&h.size, sizeof h.size);
      cout << "File size: " << h.size << endl;
    Last edited by ChuckB; Jan 19th, 2003 at 09:42 PM.
    I learn Robotics, Electronics and Game Programming at http://www.gameinstitute.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width