簡體   English   中英

為什么在 c 中沒有打印二進制文件的內容?

[英]Why are contents of a binary file not being printed in c?

您好我正在編寫一個程序,它讀取二進制文件的內容並將它們打印到屏幕上。

  #include <stdio.h> 
 #include <stdlib.h> // For exit() 

 int main() 
 { 
FILE *fptr; 

char filename[100];


printf("Enter the filename to open \n"); 
scanf("%s", filename); 

// Open file 
fptr = fopen(filename, "rb"); 
if (fptr == NULL) 
{ 
    printf("Cannot open file \n"); 
    exit(0); 
} 

// Read contents from file
fseek(fptr,0L,SEEK_END); 
int fsize = ftell(fptr);
fseek(fptr,0L,SEEK_SET); 
unsigned char  *c = malloc(fsize);
fread(c,fsize,1,fptr); 
fclose(fptr); 
printf("%s",c);

return 0; 
}

但它不打印任何東西。有人可以解釋我為什么以及如何解決這個問題。

你所嘗試的根本不是你想要達到的。

請記住printf()格式化打印的數據。 要使用%s格式正確打印,二進制數據值必須是ASCII 值,但當然它們不是.

您可能應該嘗試使用%d進行printf()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM