C++: The BYTE Class?

Started by
15 comments, last by laserbeak43 16 years ago
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?
__________________________________________________________Maybe one day i can not be affraid of venturing out of the beginners section.
Advertisement
BYTE and DWORD are Win32 types, defined in (some file included by) windows.h. A quick MSDN search should give you precise definitions.
This code has nothing to do with iostreams.

Judging by his use of BYTE and DWORD, I suspect that he is using the windows data types.
Hmm,
Everyrhings in J#. Gonna use my actual MSDN help browser from visual studio and filter out the languages.
__________________________________________________________Maybe one day i can not be affraid of venturing out of the beginners section.
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. :)
__________________________________________________________Maybe one day i can not be affraid of venturing out of the beginners section.
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.
omfg duh!!!
i could just open up visual studio(resource hog) instead of codeblock and intellisense would probably give me good info on it!!!
__________________________________________________________Maybe one day i can not be affraid of venturing out of the beginners section.
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 :)
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;}
__________________________________________________________Maybe one day i can not be affraid of venturing out of the beginners section.
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.

This topic is closed to new replies.

Advertisement