#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];
uint32_t memory_int_2[4];
char memory[4];
unsigned char memory_2[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" );
fclose(file);
return EXIT_FAILURE;
}
if ( fread( memory_int , sizeof(memory_int) , 1 , file ) != 1) {
printf("Could not read first.size\n");
fclose(file);
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" );
fclose(file);
return EXIT_FAILURE;
}
if ( fread( &first.size , sizeof(first.size) , 1 , file ) != 1) {
printf("Could not read first.size\n");
fclose(file);
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" );
fclose(file);
return EXIT_FAILURE;
}
if ( fread( memory_int_2 , sizeof(memory_int_2) , 1 , file ) != 1) {
printf("Could not read first.size\n");
fclose(file);
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" );
fclose(file);
return EXIT_FAILURE;
}
if ( fread( memory , sizeof(memory) , 1 , file ) != 1) {
printf("Could not read first.size\n");
fclose(file);
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" );
fclose(file);
return EXIT_FAILURE;
}
if ( fread( memory_2 , sizeof(memory_2) , 1 , file ) != 1) {
printf("Could not read first.size\n");
fclose(file);
exit (0);
}
printf ("This is memory_int[0] before sync_safe: %u %u %u %u\n", memory_int[0],memory_int[1],memory_int[2],memory_int[3]);
printf ("This is first.size before sync_safe: %" PRIu32"\n", first.size);
printf ("This is memory_int_2[0] before sync_safe with PRIu32: %" PRIu32" %" PRIu32" %" PRIu32" %" PRIu32"\n", memory_int_2[0],memory_int_2[1],memory_int_2[2],memory_int_2[3]);
printf ("This is memory[0] before sync_safe: %d %d %d %d\n", memory[0],memory[1],memory[2],memory[3]);
printf ("This is memory before sync_safe: %s\n", memory);
printf ("This is memory_2[0] before sync_safe: %d %d %d %d\n", memory_2[0],memory_2[1],memory_2[2],memory_2[3]);
printf ("This is memory_2 before sync_safe: %s\n", memory_2);
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);
first.size = (first.size & 0xFF);
printf ("This is first.size after sync_safe: %" PRIu32"\n", first.size);
first.size = (memory[3] & 0xFF) |
((memory[2] & 0xFF) << 7 ) |
((memory[1] & 0xFF) << 14 ) |
((memory[0] & 0xFF) << 21 );
printf ("This is first.size after sync_safe: %" PRIu32"\n", first.size);
first.size = (memory_2[3] & 0xFF) |
((memory_2[2] & 0xFF) << 7 ) |
((memory_2[1] & 0xFF) << 14 ) |
((memory_2[0] & 0xFF) << 21 );
printf ("This is first.size after sync_safe: %" PRIu32"\n", first.size);
fclose(file);
return 0;
}
当我编译代码时,我收到错误消息:
错误:下标值既不是数组也不是指针也不是向量 first.size = (first.size[3] & 0xFF) |
我收到此错误的原因是我试图编译一个像数组一样的字符串。我将它定义为uint32_t size
并且我称它为first.size[3]
. 当然它永远不会起作用,因为一个是简单的字符串,第二个是一个数组。
这是我作为样本创建的测试代码,用于测试和学习最多,以解决我的问题。自从我刚开始学习编程以来,我有很多东西要阅读理解和学习。
我希望这个代码示例能像对我一样帮助其他人。