I am new in programming and even more in c programming. I am trying to read a binary file and then do bitwise process. Sample of my code and how I have written my program so far.
#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[]) {
mp3_Header first;
unsigned int memory_int[4];
FILE *file = fopen( "song.mp3" , "rb" );
/* 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\n", memory_int);
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: %" PRIu32"\n", first.size);
fclose(file);
return 0;
}
When I am compiling the code I get the error message:
error: subscripted value is neither array nor pointer nor vector
first.size = (first.size[3] & 0xFF) |
I tried to declare another value as:
unsigned int memory_int[4];
When I compile the code I get the warning:
warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘unsigned int *’ [-Wformat=]
printf ("This is memset memory_int: %u\n", memory_int);
Although that I changed the %" PRIu32" to %u I still get this warning.
I know so far that when I am using typedef struct I can call the content by using name.parameter, and the program seems to be operating correctly when I call first.size the value when need it. But I encounter a problem when I am trying to bitwise because I am asking the value to be broken in bits. Theoretically it should be possible since I have define it as 32 bit, but practically I am missing something extremely small. I tried to assign another value as uint32_t and also as unsigned int size[4] both times not successfully. Can some explain to me the reason and how to solve this problem?
memory_intin your code?unsigned int. However, you probably want it to be anunsigned int [4].memory_int, and before first.size was being used (and incorrectly he was attempting to subscript it when it is of course not a pointer, as the compiler message warned). Actually, on second readthrough the compiler messages are quoting lines that are not in this code sample, so I'm guessing he's tried different things but didn't post all of them. @Thanos, what compiler error do you get with this particular code sample?unsigned int memory_int[4];but I still get this error:warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘unsigned int *’ [-Wformat=] printf ("This is memset memory_int: %u\n", memory_int);I have also triedunint32_t memory_int[4];and I get the same error message:warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘uint32_t *’ [-Wformat=] printf ("This is memory_int before sync_safe: %u %u %u %u\n", memory_int_2);error: subscripted value is neither array nor pointer nor vector