【发布时间】:2023-04-03 07:35:02
【问题描述】:
我试图打印出结构中的元素(.WAV 文件头)。我已经实现了字节序校正功能。但是,当我执行 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]; 整数块大小; 字符波[4]; 字符 fmt[4]; };
【问题讨论】:
-
你没有发布
struct WAV的定义,也没有投malloc()。
标签: c