converting PCM to wav file

前端 未结 1 1374
失恋的感觉
失恋的感觉 2021-01-23 04:29
#include 
#include 

int main() {

    FILE *fin,*fout;
    char buffer[1028];


    int readcount=0;
    short NumChannels = 1;
    short         


        
相关标签:
1条回答
  • 2021-01-23 05:01

    I know nothing about the file formats, but I find it hard to believe they would have a binary header followed by ascii data. So the use of fgets and fputs I find questionable since fgets looks for a \n terminator. So you could be losing data if the buffer fills up before it happens to find a \n ( 0xa ) byte.

    while(!feof(fin))
    {
        fgets(buffer,sizeof(buffer),fin);
        fputs(buffer,fout);
    }
    

    Something like this would make more sense

    size_t n;
    while((n = fread(buffer, 1, sizeof(buffer), fin)) > 0) {
       if(n != fwrite(buffer, 1, n, fout)) {
          perror("fwrite");
          exit(1);
       }
    }
    if(n < 0) {
       perror("fread");
       exit(1);
    }
    

    Also, why do you close fout and then reopen it?

    0 讨论(0)
提交回复
热议问题