• Advertisement
Sign in to follow this  

C++: The BYTE Class?

This topic is 3617 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm learning how to read wave files with IOStreams and on the page i found, the author mentions nothing of the BYTE class/keyword/thing he's using.
FILE *fp; 

fp = fopen("sound.wav","rb); 
if (fp) 
{ 
    BYTE id[4]; //four bytes to hold 'RIFF' 
    DWORD size; //32 bit value to hold file size 

    fread(id,sizeof(BYTE),4,fp); //read in first four bytes 
    if (!strcmp(id,"RIFF")) 
    { //we had 'RIFF' let's continue 
        fread(size,sizeof(DWORD),1,fp); 
        //read in 32bit size value 
    } 
} 
i'm pretty sure i know what BYTE does, but the anal in me wants to see documentation :) i've looked in my C++ IOStrams Handbook and cplusplus.com but didn't find anything. anyone willing to shell out an explanaition?

Share this post


Link to post
Share on other sites
Advertisement
BYTE and DWORD are Win32 types, defined in (some file included by) windows.h. A quick MSDN search should give you precise definitions.

Share this post


Link to post
Share on other sites
Hmm,
Everyrhings in J#. Gonna use my actual MSDN help browser from visual studio and filter out the languages.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
This code has nothing to do with iostreams.


Oh, isn't FILE a part of iostrams?

anyway, looks like i can't find anything useful(why am i not suprised). guess this is gonna be one of those "just know that it works, learn it later" things.

thanks for trying guys. :)

Share this post


Link to post
Share on other sites
Quote:
Oh, isn't FILE a part of iostrams?
No, FILE is part of the C standard library, while the stream classes are part of the C++ standard library (of course the latter is part of the former, more or less, but you know what I mean).

Basically, you're writing Windows-specific C code. I don't know what your objectives are exactly, but I would recommend taking some time to become familiar with C++, as it will make your life much easier (compared to C, that is). (There are still a lot of tutorials floating around on the net that are either pure C or 'C++ but not really', so it's easy to get misled when looking for good references on this subject.)

Also, if you're interested in writing portable code, a good first step would be replace BYTE and DWORD with their C/C++ equivalents, char (probably unsigned) and... Well, there isn't a built-in primitive type in C++ (at this time) that's guaranteed to be 32 bits wide, but you can use the typedefs (uint32 or whatever) provided in newer versions of the C standard (?) or in the Boost libraries.

Share this post


Link to post
Share on other sites
omfg duh!!!
i could just open up visual studio(resource hog) instead of codeblock and intellisense would probably give me good info on it!!!

Share this post


Link to post
Share on other sites
Oh, and you might consider adding an HTML 'horizontal rule' at the top of your signature - as is, it kind of looks like you're ending every single post with a quip about leaving the beginners' forum, which is kind of strange :)

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Oh, and you might consider adding an HTML 'horizontal rule' at the top of your signature - as is, it kind of looks like you're ending every single post with a quip about leaving the beginners' forum, which is kind of strange :)


hehe my bad.

another thing. i'm tinking i'm calling the wrong BYTE here. cause i'm getting an error telling me i need to perform a typecast.

example code:

FILE *fp;

fp = fopen("sound.wav","rb");
if (fp)
{
BYTE id[4]; //four bytes to hold 'RIFF'
DWORD size; //32 bit value to hold file size

fread(id,sizeof(BYTE),4,fp); //read in first four bytes
if (!strcmp(id,"RIFF"))
{ //we had 'RIFF' let's continue
fread(size,sizeof(DWORD),1,fp);
//read in 32bit size value
}
}




and here's my version of the code:


#include <stdio.h>
#include <windows.h>

FILE * fp;

int main()
{
fp = fopen("sound.wav","rb");
if (fp)
{
BYTE id[4]; //four bytes to hold 'RIFF'
DWORD size; //32 bit value to hold file size

fread(id, sizeof(BYTE),4,fp); //rean in first four bytes
if (!strcmp(id, "RIFF"))
{//we had 'RIFF' let's continue
fread(size, sizeof(DWORD), 1, fp);
//read in 32bit size value
}
}
return 0;
}

Share this post


Link to post
Share on other sites
His code is wrong. You can't use strcmp for two reasons:
1. You're comparing unsigned chars (Which is how a BYTE is declared)
2. You're not using a null terminated string
Best case is that it'll compile with some warnings on a non-standard compiler like VC6, worst case (And likely case) is that your app crashes horribly.

You should do something like this:
if (id[0]=='R' && id[1]=='I' && id[2]=='F' && id[3]=='F')
I.e. compare each byte. Or use memcmp or similar - Although if you're only checking 4 bytes, I'd just use the above way.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Also, if you're interested in writing portable code, a good first step would be replace BYTE and DWORD with their C/C++ equivalents, char (probably unsigned) and... Well, there isn't a built-in primitive type in C++ (at this time) that's guaranteed to be 32 bits wide, but you can use the typedefs (uint32 or whatever) provided in newer versions of the C standard (?) or in the Boost libraries.

Yargh.

Portable win32 code? Portable to what, Linux?

Share this post


Link to post
Share on other sites
Quote:
Original post by Konfusius
Quote:
Original post by jyk
Also, if you're interested in writing portable code, a good first step would be replace BYTE and DWORD with their C/C++ equivalents, char (probably unsigned) and... Well, there isn't a built-in primitive type in C++ (at this time) that's guaranteed to be 32 bits wide, but you can use the typedefs (uint32 or whatever) provided in newer versions of the C standard (?) or in the Boost libraries.

Yargh.

Portable win32 code? Portable to what, Linux?
I'm not following. I might be missing something, but other than the aforementioned typedefs, I didn't see any indication that the OP intends to write Win32-only code.

Share this post


Link to post
Share on other sites
I usually try to make all my code(not much) portable. I`m Studying cross platform GUI programming with QT4.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Quote:
Original post by Konfusius
Quote:
Original post by jyk
Also, if you're interested in writing portable code, a good first step would be replace BYTE and DWORD with their C/C++ equivalents, char (probably unsigned) and... Well, there isn't a built-in primitive type in C++ (at this time) that's guaranteed to be 32 bits wide, but you can use the typedefs (uint32 or whatever) provided in newer versions of the C standard (?) or in the Boost libraries.

Yargh.

Portable win32 code? Portable to what, Linux?
I'm not following. I might be missing something, but other than the aforementioned typedefs, I didn't see any indication that the OP intends to write Win32-only code.


Yes, sorry. I'm a little tired. I thinkg that the typedefs (BYTE and DWORD etc) are there exactly for portability reason. Using undisguised C++ types like unsigned char seems a step backwards to me.

Share this post


Link to post
Share on other sites
the whole thing ended up being wrong for what i was doing. i've tried all kinds of tutorials from MCI to libsndfile. i'll be 1000 years old before i can read a wave file. actually trying to get an 11 line program using MCI to work now. Pssh!!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement