There are some things to be modified in your code.
First, use " "
in the name of the file to be read, as the fopen
synax is
FILE *fopen(const char *path, const char *mode);
This way, the filename
[path
] will be const char *
Second, add the following lines to your code [after the existing line].
FILE *file = fopen("song.mp3" , "rb" );
if (!file)
{
printf("Unable to open the file for reading\n");
exit (0);
}
It will help you handle some unexpected conditions like "File does not exist"
Third, declare memory_int
as an array of unsigned int
containing 4 elements.
unsigned int memory_int[4] = {0};
Finally, altogether, the code looks like this.
#include <stdint.h>
#include <unistd.h>
#include <inttypes.h> /* Use C99 format specifiers: "%" PRIu8 "\n", "%" PRIu32 "\n" */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <inttypes.h> /* Use C99 format specifiers: "%" PRIu8 "\n", "%" PRIu32 "\n" */
typedef struct{
uint32_t size; /* Unsigned int 4 bytes (32 bits) */
}mp3_Header;
int main (int argc, char *argv[]) {
unsigned int memory_int[4] = {0};
mp3_Header first;
FILE *file = fopen("song.mp3" , "rb" );
if (!file)
{
printf("Unable to open the file for reading\n");
exit (0);
}
/* 1 Byte flags + revision_number 1 Byte + major_version 1 Byte + header_id 3 Bytes = 6 */
if ( fseek( file , 6 , SEEK_SET ) == -1 ) {
fprintf( stderr, "Not able to fseek at possition 6" );
return EXIT_FAILURE;
}
if ( fread( memory_int , sizeof(memory_int) , 1 , file ) != 1) {
printf("Could not read first.size\n");
exit (0);
}
printf ("This is first.size before sync_safe: %u %u %u %u\n", memory_int[0],memory_int[1],memory_int[2],memory_int[3]);
first.size = (memory_int[3] & 0xFF) |
((memory_int[2] & 0xFF) << 7 ) |
((memory_int[1] & 0xFF) << 14 ) |
((memory_int[0] & 0xFF) << 21 );
printf ("This is first.size after sync_safe: %u\n", first.size);
fclose(file);
return 0;
}
Hope this works!!