strcmp prob...

Started by
5 comments, last by henrym 21 years, 10 months ago
I've just started loading .WAV files and I've ripped some code from here, don't worry I'm going to rewrite it later.
    
void Load_Wave_File(char *fname)
{
    FILE *fp;

    fp = fopen(fname,"rb");
    if (fp)
    {
        BYTE id[4], *sound_buffer; //four bytes to hold 'RIFF'

        DWORD size; //32 bit value to hold file size

        short format_tag, channels, block_align, bits_per_sample; //our 16 values

        DWORD format_length, sample_rate, avg_bytes_sec, data_size, i; //our 32 bit values


        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

            fread(id, sizeof(BYTE), 4, fp); //read in 4 byte string now

            if (!strcmp(id,"WAVE"))
            { //this is probably a wave file since it contained "WAVE"

                fread(id, sizeof(BYTE), 4, fp); //read in 4 bytes "fmt ";

                fread(&format_length, sizeof(DWORD),1,fp);
                fread(&format_tag, sizeof(short), 1, fp); //check mmreg.h (i think?) for other 

                                                              // possible format tags like ADPCM

                fread(&channels, sizeof(short),1,fp); //1 mono, 2 stereo

                fread(&sample_rate, sizeof(DWORD), 1, fp); //like 44100, 22050, etc...

                fread(&avg_bytes_sec, sizeof(short), 1, fp); //probably won't need this

                fread(&block_align, sizeof(short), 1, fp); //probably won't need this

                fread(&bits_per_sample, sizeof(short), 1, fp); //8 bit or 16 bit file?

                fread(id, sizeof(BYTE), 4, fp); //read in 'data'

                fread(&data_size, sizeof(DWORD), 1, fp); //how many bytes of sound data we have

                sound_buffer = (BYTE *) malloc (sizeof(BYTE) * data_size); //set aside sound buffer space

                fread(sound_buffer, sizeof(BYTE), data_size, fp); //read in our whole sound data chunk

            }
            else
                printf("Error: RIFF file but not a wave file\n");
        }
        else
            printf("Error: not a RIFF file\n");
    }
}
    
My Problem is the

    if (!strcmp(id, "RIFF"))
 
Whenever I run the program this function prints out "Error: not a RIFF file" when I know the file actually is... strcmp() is returning a non-zero value meaning that id != "RIFF", right? My guess is that "RIFF" is actually a NUL-terminated string? whereas id(once loaded) is equal to "RIFF", not NUL-terminated... Am I right on this assumption? Or completely wrong? Henrym [edited by - henrym on June 8, 2002 7:53:35 AM]
Advertisement
The problem is that when system allocates memory for your BYTE id[4], nobody clears the fifth byte to zero. You must either read the id in something like
BYTE id[5];
id[4] = 0;
fread(id, 4, 1, fp);
..compare here...
..........

or use memcmp() instead of strcmp()...
I think your assumption is right. Try memcmp() instead...

Gero Gerber
quote:Original post by henrym
My guess is that "RIFF" is actually a NUL-terminated string? whereas id(once loaded) is equal to "RIFF", not NUL-terminated...


I think your right,...... you should use strncmp(), it''s exactly the same as strcmp() but it let''s you set the maximum string length.

    if (!strncmp( id, "RIFF", 4 ))    {       ....    } 


-Crawl
--------<a href="http://www.icarusindie.com/rpc>Reverse Pop Culture
Cheers guys.

Henrym
Or you can compare the values directly as an integer:

    #define IDWAVEHEADER	(('F'<<24)+('F'<<16)+('I'<<8)+'R')...fread(id, 1, 4, fp);if(id == IDWAVEHEADER){   // Good WAV file}else{   // Bad WAV file}    


[edited by - Zipster on June 8, 2002 8:22:12 PM]
I don''t know if this has been said already, but your ''string'' is only 4 bytes long, strcmp looks for a null terminator. use memcmp instead with the length=4. To be really efficient read it into a 32-bit long and compare it with RIFF in HEX form as Zipster said

all those single freads look very inefficent, can''t you just use a struct (some people just don''t like structures...)

This topic is closed to new replies.

Advertisement