839.2009.C.四.1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXN 16
char buf[MAXN];

int main()
{
FILE* fp = fopen("Microsoft.pub","rb");
if(fp == NULL)
{
perror("error");
return 0;
}
int cnt = 0;
int offset = 0;
while((cnt = fread(buf, sizeof(char), MAXN, fp)) != 0)
{
printf("%08x\t",offset);
for(int i = 0; i < cnt; ++i)
printf("%02hhx ",buf[i]);
putchar(' ');
//在最后一行后边补齐空格
for(int i = 0; i < MAXN-cnt; ++i)
printf(" ");
for(int i = 0; i < cnt; ++i)
{
if(isprint(buf[i])) putchar(buf[i]);
else putchar('.');
}
puts("");
offset += cnt;
}
return 0;
}