2013-10-16 12 views
0

因爲我用C語言編寫了一段時間,但一些奇怪的事情正在發生。也許有人可以運行我的代碼,看看輸出是否相同。它似乎不是打印零和'數據輸出'似乎不打印。從WAV文件中以十六進制格式使用C顯示數據

#include <stdlib.h> 
#include <stdio.h> 
#include <stdint.h> 
#include <io.h> 
#include <conio.h> 

void main(void) 
{ 
    int i; 
    long n; 
    FILE *wavFile; 
    printf("The data output \n\n\r"); 
    wavFile = fopen("bigbrain.wav","rb"); 
    while(feof(wavFile)==0) 
    { 
     i = getw(wavFile); 

     if(ferror(wavFile)!=0) 
     { 
      printf("\n an error has occured"); 
      n = ftell(wavFile); 
      printf("\nThe value of n is %ld",n); 
      getch(); 
     } 
    printf("%x",i); 
    } 

    n = ftell(wavFile); 
    printf("\n\The value of n is %ld",n); 
    fclose(wavFile); 
    printf("\n\nEnd of File"); 
    getch(); 
} 

我從這得到的結果是。由於它是一個波形文件,我認爲應該有一些地方持有零。有沒有人看到任何錯誤?

4646495212c524556415720746d6610100012b112b118 

這是我剛剛通過交叉refferencing什麼,我知道應該在那裏打破了數據下降了手。

//Winamp says Unsigned 8-bit PCM; 1 channel; 11025Hz 
46464952 //"RIFF" 
12c52  //size: actuall size 76890, says 76882. My doc says correct because minus 8 bits for fields not included 
45564157 //format "WAVE" 
20746d66 //subchunkID 0x666d7420 but in big endian 
10100012 //subchunk 1 size, expecting 16 
b1   //audio format 
1   //number of channels 
2b11  //sample rate: file should be 11025 (ox2b11) 
8000  //byte rate 
      //block align 
1   //bits per sample... i think it cut off zero so 0x10 for 16 
61746164 //start of data it's "data" 0x64617461 in big endian 
+1

除非'getw'實際返回錯誤,否則您不應該檢查'ferror'。另外,'getw'是一個陳舊和過時的功能,你不應該在現代程序中使用它。 –

+1

你也在做錯誤的讀取循環。我建議你改用'fread'來讀取數據,然後使用'while(fread(&i,sizeof(i),1,wavFile)!= EOF){printf(...); }'。 'fread'在文件結尾*或*錯誤處返回'EOF'。 –

+1

另請注意,根據字節排序,您打印的數據可能看起來有誤。而且有些數據實際上不是32位值,而是8位或16位。 –

回答

1

變化

printf("%x",i); 

printf("%08x, i); 

這將確保8位數字被印刷,帶前導零。

此外,解析您的波形文件時,你可能會fread頭到緩衝區中,您將檢查,而不是在!feof()循環工作逐字節。

1 - 這裏實際上是逐字,因爲您使用的是getw,我甚至不知道它存在。

相關問題