- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Microsoft
- >
- >>>Fail to Use C to Open the WAV File <<<
Operating System - Microsoft
1748178
Members
4208
Online
108758
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2003 04:43 AM
07-25-2003 04:43 AM
>>>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!!
#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<<<
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP