Operating System - Microsoft
1748178 Members
4208 Online
108758 Solutions
New Discussion

>>>Fail to Use C to Open the WAV File <<<

 
Coffee
Occasional Contributor

>>>Fail to Use C to Open the WAV File <<<

Hi!I want to use the C language to open a WAV file,extract the data chunk and copy the data chunk to another new binary file.Thus in reference of the existed resource in the internet and with my own mind,I wrote the code as follows.But,in executing this code,it seems that the"another new binary file"(I created a blank WAV file to receive the data,is this the right way?)cannot be wrotten with the data chunk of the WAV which I do operation on.Why?How can I solve the problem?
#include
typedef struct
{
unsigned a:8;
}BYTE;

void Load_Wave_File(char *fname,char *fname1)
{
FILE *fp,*fp1;
BYTE id[4], *sound_buffer; /*four bytes to hold 'RIFF'*/
long size; /*32 bit value to hold file size*/
short format_tag, channels, block_align, bits_per_sample; /*our 16 values*/
long format_length, sample_rate, avg_bytes_sec, data_size; /*our 32 bit values*/
fp=fopen(fname,"rb");
if(fp)
{
fread(id, sizeof(BYTE), 4, fp); /*read in first four bytes*/
if (!strcmp(id, "RIFF"))
{
/*we had 'RIFF' */
fread(&size, sizeof(long), 1, fp); /*read in 32bit size value*/
fread(id, sizeof(BYTE), 4, fp); /*read in 4 byte string now*/
if (!strcmp(id,"WAVE"))
{
/*this is probably a wave file since it contained "WAVE"*/
fread(id, sizeof(BYTE), 4, fp); /*read in 4 bytes "fmt ";*/
fread(&format_length, sizeof(long),1,fp);
fread(&format_tag, sizeof(short), 1, fp);

fread(&channels, sizeof(short),1,fp); /*mono,stereo*/
fread(&sample_rate, sizeof(long), 1, fp); /*ike 44100, 22050, etc...*/
fread(&avg_bytes_sec, sizeof(short), 1, fp);
fread(&block_align, sizeof(short), 1, fp);
fread(&bits_per_sample, sizeof(short), 1, fp); /*8 bit or 16 bit file?*/
fread(id, sizeof(BYTE), 4, fp); /*read in 'data'*/
if(!strcmp(id,"data"))
{
fread(&data_size, sizeof(long), 1, fp); /*how many bytes of sound data*/
while(! feof(fp))fputc(fgetc(fp),fp1); /*copy sound data chunk*/
}
else printf("Error:this is not the DATA chunk\n");
}
else
printf("Error: RIFF file but not a wave file\n");
}
else
printf("Error: not a RIFF file\n");
}
}
main(){
char fname[12],fname1[12];
printf("please input the infile name:\n");
gets(fname);
printf("please input the outfile name:\n");
gets(fname1);
Load_Wave_File(fname,fname1);
}
>>>>>ALSO,I WROTE ANOTHER CODE AS FOLLOWS:<<<<<
#include
typedef struct
{
unsigned a:8;
}BYTE;

main(int argc,char *argv[]){
FILE *in,*out;
int i=0;
BYTE id[2];
long data_size;
if(argc!=3){
printf("you forget to enter a filename\n");
exit(0);
}
if((in=fopen(argv[1],"rb"))==NULL)
{printf("cannot open infile\n");
exit(0);
}
if((out=fopen(argv[2],"wb"))==NULL)
{printf("cannot open outfile\n");
exit(0);
}
while(strcmp(id,"da")){
fread(id,sizeof(BYTE),2,in);
i++;
i++;
}
fread(&data_size,sizeof(long),1,in);i=i+4;
while(! feof(in))fputc(fgetc(in),out);
fclose(in);
fclose(out);
}
In executing this code,the another file which is a new-created WAV file(opened by pointer"out")becomes totally blank(0kb),so there must be some operation on it,but the data chunk is not wrotten into it too.Why??
How can I improve it?
Thank you so much!!


>>>Undergraduate student majoring in Communication Engineering<<<