Connecting Tech Pros Worldwide Forums | Help | Site Map

.wav file reading

Newbie
 
Join Date: Sep 2006
Posts: 3
#1: Sep 6 '06
i was able to open the .wav file using c program, but while reading,its not reading the full file.i opened the .wav file in visual cpp and compared it with the program output.so many data missing.its reading only less than 1/4th of the file.no error is shown by the program.so how can i read a wav file correctly in the hex format.my program is given below.
[code]
#include<stdio.h>
#include<conio.h>

void main()

{
int i;
long n;
FILE *f1;
clrscr();
printf("the data output is \n\n");
f1=fopen("silence.wav","r");
while(feof(f1)==0)
{
i=getw(f1);
if(ferror(f1)!=0)
{
printf("\n an error has occurred"); just to check if any error occured.
n=ftell(f1);
printf("\n\nthe value of n is %ld",n);
getch();
}
printf("%x ",i);
}
n=ftell(f1);
printf("\n\nthe value of n is %ld",n);
fclose(f1);
printf("\n\n end of the file");
getch();
}
[code]

Newbie
 
Join Date: Sep 2006
Posts: 4
#2: Sep 6 '06

re: .wav file reading


hai Joby,
Your problem is simple.Here you are trying to open the file in read mode using the "r" in fopen().This mode is generally used for text files .the .wav is not text file.So that you should open it in binary read mode using "rb" or "rb+" instead of "r" in fopen()
Member
 
Join Date: Sep 2006
Posts: 61
#3: Sep 6 '06

re: .wav file reading


I will suggest you to go through the open,read,write,close functions in c.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,185
#4: Sep 6 '06

re: .wav file reading


I do not think that getw is an ANSI function and is certainly non-portable since it reads an integer which may differ from system to system in both size and endianess.

An improved method would be to use fread to read in the number of bytes and then to reconstruct the integer from that

Expand|Select|Wrap|Line Numbers
  1. unsigned char byte[4];
  2.  
  3. if (fread(byte, sizeof byte, 1, f) == sizeof byte)
  4. {
  5.     int i = byte[0] | (byte[1]<<8) | (byte[2]<<16) | (byte[3]<<24);
  6. }
  7.  
Newbie
 
Join Date: Sep 2006
Posts: 4
#5: Sep 7 '06

re: .wav file reading


Hai joby,
you can do this by opening it in binary mode ,the code may be as follows
#incude<io.h>
//add additional headder files
void main()
{
int han,c;
char file[20],buf[20];
gets(file);
han=open(file,O_RDONLY|O_BINARY);
while(1)
{
c=read(han,&buf,10);
if(c==0)
{
break();
}
//manipulate the characters upto buf[c-1]
}
}
Reply