Jump to content
  • Advertisement
Sign in to follow this  
laserbeak43

C++: The BYTE Class?

This topic is 3877 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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!