在那裏,我有這樣的任務分配時,我想模仿從UNIX的hexdump都功能。我得到我的程序轉儲文件到十六進制,但我無法打印偏移量和可打印的字符。我怎麼會打印出像進制打印功能的地址在UNIX
這裏是我的代碼:
/*performs a hex and an octal dump of a file*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define LINESIZE 512
void hexdump(FILE *fp);
void octaldump(FILE *fp);
int main(int argc, char * argv[])
{
FILE *fp;
if((fp = fopen(argv[1], "rb+")) == '\0')
{
perror("fopen");
return 0;
}
hexdump(fp);
return 0;
}
void hexdump(FILE *fp)
{
char temp [LINESIZE];
size_t i = 0;
size_t linecount = 1;
long int address = 0;
while(fscanf(fp," %[^\n] ", temp) == 1)
{
printf("%5d", address);
for(i = 0; i < strlen(temp); i++)
{
printf("%02x ", temp[i]);
if(linecount == 16)
{
printf("\n");
linecount = 0;
}
linecount++;
}
printf(" | ");
for(i = 0; i < strlen(temp); i++)
{
if(temp[i] < 32)
{
printf(".");
}
else
{
printf("%c", temp[i]);
}
}
printf("\n");
}
}
正如我前面所說,我的計劃是想打印的offset,則文件的十六進制值0的,那麼像這樣的文件的可打印字符填充。
0000000 7f 2a 34 f3 21 00 00 00 00 00 00 00 00 00 00 00 | test file
我設法打印出可打印字符,但它們出現在十六進制部分之後。因此,如果一行十六進制代碼擴展到下一行,它將在下一行打印可打印字符。
例如:
2a 3b 4d 3e 5f
12 43 23 43 | asfdg df
我如何獲得它打印任何字符的十六進制字符的一個行後會出現?
PS:出於某種原因,我的程序不墊0的出於某種原因。
EDIT1:我得到了抵消一部分,我只是不斷添加16到我的地址變量,並保持印刷
insteading使用的fread的,我可以用龜etc然後限制被放在屏幕chracters的數字字符閱讀其性質? – user830812
@ user830812:我想你可以但我不知道你爲什麼想要。在產生任何輸出之前,你需要一整行的字節值,所以爲什麼不一次只有'fread' 16個字節呢? –
我試圖取代用fread while循環的sscanf的一部分,但我不認爲它會通過迴路現在 – user830812