I had this code which initially reads a .wav file to find out the number of samples in it. Using this size I was trying to make array of that size. After that I was reading the samples form same .wav file and was storing in that array, but out of 762880 samples it was reading only 7500 samples(approx.).
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void main(){
FILE *fp;
long n,i=0;
float *data;
FILE* inp =NULL;
FILE* oup =NULL;
float value =0;
signed short ss;
/* Open file */
fp = fopen("k1s1.wav", "rb");
fseek(fp, 0L, SEEK_END);
n = ftell(fp);
n=n/2-22;
printf("n:%ld",n);
fclose(fp);
data=malloc(n*sizeof(float));
inp = fopen("k1s1.wav","rb");
oup = fopen("cs123.txt","wt");
fseek (inp,44,SEEK_SET);// that didn't help getting the right result !!
i=0;
for(i=0;i<n;i++){
fread(&ss, sizeof(signed short) ,1 , inp);
//Now we have to convert from signed short to float:
value=((float)ss)/(unsigned)0xFFFF;
value= value* 2.0;
value= value*100000;
value= ceil(value);
value= value/100000;
fprintf(oup,"%f\n",value);
data[i]=value;
///printf("%lf\t",value);
}
fclose(inp);
fclose(oup);
printf("done");
}
When I remove this line - "data[i]=value;" in for loop the programs works fine and i can see output in the file.I need to store these values in array as well for further computations. What could be the error?