0

I want to read a wav file & change its bit per sample rate(from 16 to 32). But my program is not copying entire file. Source file is 175KB where as destination file is only 2KB. Bits per sample is at 34 bytes from beginning.

My code is:-

#include<stdio.h>
void main()
{
 FILE *fp,*fo;
 char ch,ch1;
 int j=0,s=0,arr[4],k=0;
 long int i=0;
 fp=fopen("msoft.wav","rb");
 fo=fopen("dest.wav","wb");
 while(1)
 {
 i=i+1;
 ch=fgetc(fp);
 if(ch==EOF)break;
 else
  {
    if(i==34)
    {
    while(i<=35)
    {
     ch=fgetc(fp);
        arr[j]=ch;
        i++;
        j++;
    }
    for(k=0;k<2;k++)
    printf("\n%d",arr[k]);
     s=arr[1];
     s=(s<<8)+arr[0];
     printf("\n\nS=%d",s);
     s=s*2;
     printf("\n new s=%d",s);
     ch1=s & 255;
     fputc(ch1,fo);
     ch1=s & (255<<8);
     fputc(ch1,fo);
    }
    else
        fputc(ch,fo);
 }
 }
 printf("\nOk");
 getch();
}

Please help.

Mat
  • 202,337
  • 40
  • 393
  • 406
saurabh
  • 65
  • 1
  • 3
  • 7

1 Answers1

3

fgetc returns an int, not a char. You absolutely need to save its return value into an int otherwise there will be no difference between an plain 0 in the file and EOF.

See related question: fgetc does not identify EOF

Community
  • 1
  • 1
Mat
  • 202,337
  • 40
  • 393
  • 406
  • @saurabh, update your question to show how you changed it, and be more precise when you say "not working" - please post the exact size of the generated output, and also make sure your code formatting is readable, the indentation is pretty bad as it is. – Mat May 17 '11 at 18:23