|
-
Jan 19th, 2003, 07:41 PM
#1
Thread Starter
Addicted Member
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.
-
Jan 19th, 2003, 08:29 PM
#2
Thread Starter
Addicted Member
Hi,
I found an answer to question #2. How to read a line of binary data using 'read' into a structure.
HEADER h;
in.read((char*)&h,14);
Regards,
ChuckB
-
Jan 19th, 2003, 09:41 PM
#3
Thread Starter
Addicted Member
Hi,
I worked out the answer to question #1.
ifstream *p = new ifstream("stone1.bmp",ios::binary | ios::in);
p->read((char*)&h,14);
Regards,
ChuckB
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|