473,320 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Trying to extract different types of data from a singe file.

I am trying to create a windows program that reads binary graphics as
a resource. This has nothing to do with win32 but conversion of data
with memcpy.

graphic::graphic(UINT uiResID, HINSTANCE hinstance){
size = 32;
bitData.clear();
void * p = NULL; // point to the data
int end;
BYTE data;

static HGLOBAL hglob;
HRSRC hRes =
FindResource(hinstance,MAKEINTRESOURCE(uiResID),TE XT("BINARY"));

if(hRes){
hglob = LoadResource(hinstance,hRes);
p = LockResource(hglob);
memcpy((int*) &end, p, sizeof(int)); // puts the first but of
data to int.

for(int lp = 0; lp != end; lp++){
bitData.push_back(0);
}

for(int lp = 0; lp != end; lp++){
memcpy((BYTE*) &bitData[lp], p, sizeof(BYTE)); puts the rest
of the data in BYTE type.
}

}
set();
create();
}

My question is how do I extract each but of data from p first to the
int type with is the number of data bits then each bit of data to put
into my bitData vector. I can't increase the pointer p++ to get to
the next bit of data. This compiles and runs but does not extract the
data for me.
Aug 1 '08 #1
7 2086
On Aug 1, 5:47 am, JoeC <enki...@yahoo.comwrote:
I am trying to create a windows program that reads binary
graphics as a resource. This has nothing to do with win32 but
conversion of data with memcpy.
memcpy doesn't convert; it just copies bits. If the original
data came from an external source, that's generally not what is
needed.
graphic::graphic(UINT uiResID, HINSTANCE hinstance){
size = 32;
bitData.clear();
void * p = NULL; // point to the data
int end;
BYTE data;
static HGLOBAL hglob;
HRSRC hRes =
FindResource(hinstance,MAKEINTRESOURCE(uiResID),TE XT("BINARY"));

if(hRes){
hglob = LoadResource(hinstance,hRes);
p = LockResource(hglob);
memcpy((int*) &end, p, sizeof(int)); // puts the first but of
data to int.
Note that you've got a reintepret_cast here. That's a sure sign
that something is wrong.

How are the integer values formatted in the file? Until you
know that, you can't do anything reasonable.
for(int lp = 0; lp != end; lp++){
bitData.push_back(0);
}
for(int lp = 0; lp != end; lp++){
memcpy((BYTE*) &bitData[lp], p, sizeof(BYTE)); puts the rest
of the data in BYTE type.
If BYTE is unsigned char, memcpy might just work. But did you
really mean to not increment p in the loop? If so, this is
just:

bitData.insert( bitData.end(), end, *p ) ;

would do the trick (without the previous loop. Otherwise,
you could replace the two loops with:

bitData.resize( end ) ;
memcpy( &bitData[ 0 ], p, end ) ;

(In both cases, of course, only if BYTE is unsigned char, and
bitData is std::vector< BYTE >.)

[...]
My question is how do I extract each but of data from p first
to the int type with is the number of data bits then each bit
of data to put into my bitData vector. I can't increase the
pointer p++ to get to the next bit of data.
What is the format of the input? Until we know that, we can't
really say anything. A lot of formats---almost all, I would
imagine---do pack bitmaps as 8 bits to an unsigned char. If
such is the case, and end gives the number of bits, you'll have
to scale it before using it with memcpy or your loops. On the
other hand, I wouldn't be surprised if some graphic formats have
"bitmaps" that aren't actually bitmaps: bitmaps were the most
basic graphic representation back in the days of black and
white, where a pixel was one bit, but I would imagine that a lot
of graphic formats today would use a string or an array of
pixels, rather than a true bitmap.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 1 '08 #2
On Aug 1, 4:41 am, James Kanze <james.ka...@gmail.comwrote:
On Aug 1, 5:47 am, JoeC <enki...@yahoo.comwrote:
I am trying to create a windows program that reads binary
graphics as a resource. This has nothing to do with win32 but
conversion of data with memcpy.

memcpy doesn't convert; it just copies bits. If the original
data came from an external source, that's generally not what is
needed.
graphic::graphic(UINT uiResID, HINSTANCE hinstance){
size = 32;
bitData.clear();
void * p = NULL; // point to the data
int end;
BYTE data;
static HGLOBAL hglob;
HRSRC hRes =
FindResource(hinstance,MAKEINTRESOURCE(uiResID),TE XT("BINARY"));
if(hRes){
hglob = LoadResource(hinstance,hRes);
p = LockResource(hglob);
memcpy((int*) &end, p, sizeof(int)); // puts the first but of
data to int.

Note that you've got a reintepret_cast here. That's a sure sign
that something is wrong.

How are the integer values formatted in the file? Until you
know that, you can't do anything reasonable.
for(int lp = 0; lp != end; lp++){
bitData.push_back(0);
}
for(int lp = 0; lp != end; lp++){
memcpy((BYTE*) &bitData[lp], p, sizeof(BYTE)); puts the rest
of the data in BYTE type.

If BYTE is unsigned char, memcpy might just work. But did you
really mean to not increment p in the loop? If so, this is
just:

bitData.insert( bitData.end(), end, *p ) ;

would do the trick (without the previous loop. Otherwise,
you could replace the two loops with:

bitData.resize( end ) ;
memcpy( &bitData[ 0 ], p, end ) ;

(In both cases, of course, only if BYTE is unsigned char, and
bitData is std::vector< BYTE >.)

[...]
My question is how do I extract each but of data from p first
to the int type with is the number of data bits then each bit
of data to put into my bitData vector. I can't increase the
pointer p++ to get to the next bit of data.

What is the format of the input? Until we know that, we can't
really say anything. A lot of formats---almost all, I would
imagine---do pack bitmaps as 8 bits to an unsigned char. If
such is the case, and end gives the number of bits, you'll have
to scale it before using it with memcpy or your loops. On the
other hand, I wouldn't be surprised if some graphic formats have
"bitmaps" that aren't actually bitmaps: bitmaps were the most
basic graphic representation back in the days of black and
white, where a pixel was one bit, but I would imagine that a lot
of graphic formats today would use a string or an array of
pixels, rather than a true bitmap.

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
I created the binary files in the object the standard way:

void graphic::save(std::ofstream& f){
f.write((char*)&number, sizeof(int));
for(int lp = 0; lp != (size * number); lp++){
f.write((char*)&bitData[lp], sizeof(BYTE));
}
}

void graphic::load(std::ifstream& f){
f.read((char*)&number, sizeof(int));
int end = size * number;

for(int lp = 0; lp != end; lp++){
bitData.push_back(0);
}

for(int lp = 0; lp != end; lp++){
f.read((char*)&bitData[lp], sizeof(BYTE));
}
set();
create();
}

I wrote a binary file.
I can also read the data fine from a file. I am just trying to load
in the binary files as a resource to make my program better so that I
don't have to include graphics they can already be in the complied
code. I hope this helps. I don't have good references for trying to
extract data like this.
Aug 1 '08 #3
On Aug 1, 5:56 pm, JoeC <enki...@yahoo.comwrote:
On Aug 1, 4:41 am, James Kanze <james.ka...@gmail.comwrote:
I created the binary files in the object the standard way:
void graphic::save(std::ofstream& f){
f.write((char*)&number, sizeof(int));
for(int lp = 0; lp != (size * number); lp++){
f.write((char*)&bitData[lp], sizeof(BYTE));
}
}
In other words, you have no idea of its format, and you don't
know how to read it.
void graphic::load(std::ifstream& f){
f.read((char*)&number, sizeof(int));
int end = size * number;
for(int lp = 0; lp != end; lp++){
bitData.push_back(0);
}
for(int lp = 0; lp != end; lp++){
f.read((char*)&bitData[lp], sizeof(BYTE));
}
set();
create();
}
I wrote a binary file.
But you don't know what you put into it. That doesn't advance
us very much. (Note that there's a reinterpret_cast here as
well. Bad sign.)
I can also read the data fine from a file.
You can read the bits. You don't know what they mean.
I am just trying to load in the binary files as a resource to
make my program better so that I don't have to include
graphics they can already be in the complied code.
So start by defining the format you want to use. Or use some
pre-established format. (I tend to use XDR by default.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 1 '08 #4
On Aug 1, 1:14 pm, James Kanze <james.ka...@gmail.comwrote:
On Aug 1, 5:56 pm, JoeC <enki...@yahoo.comwrote:
On Aug 1, 4:41 am, James Kanze <james.ka...@gmail.comwrote:
I created the binary files in the object the standard way:
void graphic::save(std::ofstream& f){
f.write((char*)&number, sizeof(int));
for(int lp = 0; lp != (size * number); lp++){
f.write((char*)&bitData[lp], sizeof(BYTE));
}
}

In other words, you have no idea of its format, and you don't
know how to read it.
void graphic::load(std::ifstream& f){
f.read((char*)&number, sizeof(int));
int end = size * number;
for(int lp = 0; lp != end; lp++){
bitData.push_back(0);
}
for(int lp = 0; lp != end; lp++){
f.read((char*)&bitData[lp], sizeof(BYTE));
}
set();
create();
}
I wrote a binary file.

But you don't know what you put into it. That doesn't advance
us very much. (Note that there's a reinterpret_cast here as
well. Bad sign.)
I can also read the data fine from a file.

You can read the bits. You don't know what they mean.
I am just trying to load in the binary files as a resource to
make my program better so that I don't have to include
graphics they can already be in the complied code.

So start by defining the format you want to use. Or use some
pre-established format. (I tend to use XDR by default.)

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
I didn't realize it was so complicated loading user created files as
resources. That is beyond my skill level or knowledge of the
language. Thanks for the help, I though that loading files was a
standard operation. If not I will not make my graphics a resource.
Aug 1 '08 #5
On Aug 2, 1:40 am, JoeC <enki...@yahoo.comwrote:
On Aug 1, 1:14 pm, James Kanze <james.ka...@gmail.comwrote:
I didn't realize it was so complicated loading user created
files as resources.
It's not that complicated, but... the language does not define
any binary formats (probably because there are so many to choose
from); when you do binary IO, you have to take charge of the
formatting yourself.

< That is beyond my skill level or knowledge of the
language. Thanks for the help, I though that loading files
was a standard operation.
Reading a file is. But whether text or binary, you really have
to define some sort of format. (Just outputting "file << i << j
<< k", where i, j and k are ints, won't give you something you
are read back in either.)

I might add that this is a basic software engineering problem,
and not particular to C++.

Anyway, your case is particularly simple, because you are
dealing mainly with "bytes", i.e. with raw memory, where each
byte is an element unto itself. Which is the exceptional case
where memcpy works, and istream::read and ostream::write do
everything. So you're only real problem is the initial length.
For that, I'd use an unsigned (rather than signed), although in
practice, unless you later have to port to very exotic machines,
it doesn't matter, and XDR format, which is simply four bytes,
high byte first, IOW:

to write:

void
writeInt(
std::ostream& dest,
unsigned value )
{
dest.put( (value >24) & 0xFF ) ;
dest.put( (value >16) & 0xFF ) ;
dest.put( (value > 8) & 0xFF ) ;
dest.put( (value ) & 0xFF ) ;
}

to read:

void
readInt(
std::istream& source ;
unsigned& value )
{
unsigned result = 0 ;
result |= dest.get() << 24 ;
result |= dest.get() << 16 ;
result |= dest.get() << 8 ;
result |= dest.get() ;
if ( dest ) {
value = result ;
}
}
If not I will not make my graphics a resource.
Is that portable?

Do you need portability? I'm not too familiar with the Windows
platform. But from what I understand, a resource can be bundled
into the same file as your executable. Which is a definite
advantage when it comes to deployment. (Under Unix, I have my
own programs which serve more or less the same purpose: they
convert the "resource" to C++ data declarations, which I then
compile and link into the program. It's not the same thing, but
it serves the same purpose: to make the entire application a
single file, so you don't end up with a mixture of versions or
something partially installed.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 2 '08 #6
On Aug 2, 2:53 am, James Kanze <james.ka...@gmail.comwrote:
On Aug 2, 1:40 am, JoeC <enki...@yahoo.comwrote:
On Aug 1, 1:14 pm, James Kanze <james.ka...@gmail.comwrote:
I didn't realize it was so complicated loading user created
files as resources.

It's not that complicated, but... the language does not define
any binary formats (probably because there are so many to choose
from); when you do binary IO, you have to take charge of the
formatting yourself.

< That is beyond my skill level or knowledge of the
language. Thanks for the help, I though that loading files
was a standard operation.

Reading a file is. But whether text or binary, you really have
to define some sort of format. (Just outputting "file << i << j
<< k", where i, j and k are ints, won't give you something you
are read back in either.)

I might add that this is a basic software engineering problem,
and not particular to C++.

Anyway, your case is particularly simple, because you are
dealing mainly with "bytes", i.e. with raw memory, where each
byte is an element unto itself. Which is the exceptional case
where memcpy works, and istream::read and ostream::write do
everything. So you're only real problem is the initial length.
For that, I'd use an unsigned (rather than signed), although in
practice, unless you later have to port to very exotic machines,
it doesn't matter, and XDR format, which is simply four bytes,
high byte first, IOW:

to write:

void
writeInt(
std::ostream& dest,
unsigned value )
{
dest.put( (value >24) & 0xFF ) ;
dest.put( (value >16) & 0xFF ) ;
dest.put( (value > 8) & 0xFF ) ;
dest.put( (value ) & 0xFF ) ;
}

to read:

void
readInt(
std::istream& source ;
unsigned& value )
{
unsigned result = 0 ;
result |= dest.get() << 24 ;
result |= dest.get() << 16 ;
result |= dest.get() << 8 ;
result |= dest.get() ;
if ( dest ) {
value = result ;
}
}
If not I will not make my graphics a resource.

Is that portable?

Do you need portability? I'm not too familiar with the Windows
platform. But from what I understand, a resource can be bundled
into the same file as your executable. Which is a definite
advantage when it comes to deployment. (Under Unix, I have my
own programs which serve more or less the same purpose: they
convert the "resource" to C++ data declarations, which I then
compile and link into the program. It's not the same thing, but
it serves the same purpose: to make the entire application a
single file, so you don't end up with a mixture of versions or
something partially installed.)

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Thanks for the explanation, I will save all of this and try to work
with my program. I can load in the files as binary files but trying
to do it as resources is a but different. I was advised to use strcpy.
Aug 2 '08 #7
On Aug 2, 8:04 pm, JoeC <enki...@yahoo.comwrote:
On Aug 2, 2:53 am, James Kanze <james.ka...@gmail.comwrote:
[...]
Thanks for the explanation, I will save all of this and try to
work with my program. I can load in the files as binary files
but trying to do it as resources is a but different. I was
advised to use strcpy.
If you're working with binary data (i.e. your array really is a
bitmap), strcpy will NOT work. At all. Don't listen to whoever
told you that.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 2 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Shakil Khan | last post by:
Hi there ... My question is about Meta Data which is automatically saved with files. For example,when an MS Office Documents is saved, it automaticaly save some extra information with the file...
0
by: Shakil Khan | last post by:
Hi there ... My question is about Meta Data which is automatically saved with some files. For example,when an MS Office Documents is saved, it automaticaly save some extra information with the...
8
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is,...
6
by: NutsAboutVB | last post by:
Hello, I am a .NET programmer and I have a JPEG image file (from digital camera) of about 109 KB's in size, when I open it and save it (without making any alterations at all, just going to File...
0
by: napolpie | last post by:
DISCUSSION IN USER nappie writes: Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file. This file is long file and it's composed by...
3
by: maylee21 | last post by:
hi, anyone can help me figure out how to read data from a text file like this: 10980012907200228082002 and extract the data according to this kind of format: Record type 1 TY-RECORD ...
1
by: sankar999 | last post by:
Hi All, Please help me in writing the code for extracting the data values from the XML file using C++ code. The xml file is as follows <?xml version="1.0" encoding="utf-8"?> <Settings...
4
by: nanabuch | last post by:
Hello, I am new to this forum, and I am a newbit in Oracle, I do have a background in MS Access, VBA, SQL server development and some Oracle backend development work. I have been giving a task...
1
by: veer | last post by:
Hi i am making a program in which i want to extract data from html file . Actually there are two dates on html file i want to extract these dates but the main probleum is that these dates are...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.