我試圖打印結構中的元素(.WAV文件頭)。我已經實現了endian校正功能。但是,當我做printf時,它顯示了一個奇怪的元素重複。誰能幫忙?打印組件的結構給出奇怪的重複
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "prog9.h"
/*
* little_endian_2 - reads 2 bytes of little endian and reorganizes it into big endian
* INPUTS: fptr - pointer to the wav file
* OUTPUTS: none
* RETURNS: the data that is converted to big endian
*/
int little_endian_2(FILE *fptr)
{
int count;
char temp[2];
fscanf (fptr, "%2c",temp);
char holder;
holder = temp[1];
temp[1] = temp[0];
temp[0] = holder;
count = atoi(temp);
return count;
}
/*
* little_endian_4 - reads 4 bytes of little endian and reorganizes it into big endian
* INPUTS: fptr - pointer to the wav file
* OUTPUTS: none
* RETURNS: the data that is converted to big endian
*/
int little_endian_4(FILE *fptr)
{
char temp[4];
fscanf (fptr, "%4c", temp);
int final = *(int *)temp;
//printf ("%i\n",final);
return final;
}
/*
* read_file - read the wav file and fill out the wav file struct
* INPUTS: wavfile - a string that contains the name of the file
* OUTPUTS: none
* RETURNS: the pointer to the wav file struct created in this function
* SIDE EFFECT: prints the information stored in the wav struct
*/
WAV *read_file(char *wavfile)
{
WAV* wav_ptr = (WAV*)malloc(sizeof(WAV));
FILE *fp;
fp = fopen(wavfile,"r");
fscanf (fp, "%4c", wav_ptr->RIFF); //For RIFF
wav_ptr->ChunkSize = little_endian_4(fp);
fscanf (fp, "%4c", wav_ptr->WAVE); //For WAVE
fscanf (fp, "%4c", wav_ptr->fmt); //For fmt
printf("%s\n", wav_ptr->RIFF);
printf("%i \n", wav_ptr->ChunkSize);
printf("%s \n", wav_ptr->WAVE);
printf("%s \n", wav_ptr->fmt);
return wav_ptr;
}
運行後,它將打印到輸出。
RIFFvu
882038
WAVEfmt
fmt
的結構是這樣的: 結構wav_t { 炭RIFF [4]; int ChunkSize; char WAVE [4]; char fmt [4]; };
你沒有張貼的'結構WAV'的定義,不投'的malloc()'。 –