Thread: Interesting byte read

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    60

    Question Interesting byte read

    I've just try to check a MS Multimedia signature; specifically, a WAV file (RIFF) as a test.

    So the header contains first 4-byte like: RIFF

    This is my little script:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define byte	char
    #define word	short int
    #define dword	int
    
    #define DEBUG
    int
    main( int argc, char *argv[] )
    {
    	FILE *file;
    	const char *fname = "sound.wav";
    	const char *mode  = "rb";
    	byte id[4] = { 0 };
    	int i;
    
    	file = fopen( fname, mode );
    	if( file == NULL ) {
    		printf("[error] file not found.\n");
    		exit( 1 );
    	}
    
    	fread( id, sizeof( byte ), 4, file );
    
    #if defined DEBUG
    	printf("[DEBUG]\n");
    	for( i = 0; i < 4; ++i )
    		printf("%c", id[i]);
    	printf("\nString: %s", id);
    	printf("\nLength: %d\n", strlen(id));
    	printf("[/DEBUG]\n");
    #endif
    
    	if( 0 == strcmp( id, "RIFF") )
    		printf("RIFF type\n");
    	else
    		printf("non-RIFF type\n");
    
    	fclose( file );
    	return 0;
    }
    Here the result:

    Code:
    RIFF
    String: RIFF�t	��
    Length: 17
    [/DEBUG]
    non-RIFF type
    The ID is supposed to have 4-byte size in length, but the debug result showed up w/ 17 bytes in length.

    Environment: Linux/Mandriva 2k9.1

    Do you know what's wrong w/ code? What's the result running under your system?
    Just try w/ any WAV file.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bvkim View Post
    Do you know what's wrong w/ code?
    fread() does not add a '\0', and id does not have room for one either. Change this:
    Code:
    byte id[5] = { 0 };
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf function won't read my float value...?
    By qpsnhalfs in forum C Programming
    Replies: 8
    Last Post: 07-07-2009, 11:01 PM
  2. Noob Q: How to read a value of a byte in binary?
    By Hitsuin in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 02:46 PM
  3. bytes lost with partial read in UDP
    By mynickmynick in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-31-2009, 02:06 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Byte confusion!!! HELP!
    By MrDoomMaster in forum C++ Programming
    Replies: 10
    Last Post: 11-12-2003, 04:54 PM