我似乎无法打印任何字符,事实上,程序似乎无法正确读取任何文件,因为当我给它一个文件时,所有十六进制值都是零。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <argp.h>
#include <sys/types.h>
#include <sys/stat.h>
static error_t parse_opt(int key, char *arg, struct argp_state *state);
static void dump_hex(char *f, size_t l);
static struct argp_option options[] = {
{"filepath", 'f', "PATH", 0, "uses filepath provided by user" },
{ 0 }
};
struct arguments {
char *path;
/* int *column_size; */
};
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
struct arguments *arguments = state->input;
switch(key) {
case 'f':
arguments->path = arg;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = { options, parse_opt, NULL, NULL };
static void dump_hex(char *f, size_t l) {
FILE *fd;
size_t i;
unsigned char *b = (unsigned char *)malloc(16 * sizeof(unsigned char));
memset(b, 0, 16 * sizeof(unsigned char));
if((fd = fopen(f, "r"))) {
for(i = 0; i <= l; i++) {
if((i % 8) == 0) {
if(i != 0) {
printf("| %sn", b);
}
/* print the offset */
printf("%05lx: ", i);
}
/* check if ASCII is printable */
b[i % 16] = isprint(b[i]) ? b[i] : '.';
/* print ASCII */
printf("%02x ", b[i]);
}
/* print remaining ASCII */
printf("| %sn", b);
}
fclose(fd);
free(b);
}
int main(int argc, char *argv[]) {
struct arguments arguments;
struct stat sb;
off_t size;
arguments.path = "-";
argp_parse(&argp, argc, argv, 0, 0, &arguments);
stat(arguments.path, &sb);
size = sb.st_size;
dump_hex(arguments.path, size);
return 0;
}
这是我在给它一个二进制参数时收到的输出:
./hexdump --filepath=/tmp/a.out
212d0: 00 00 00 00 00 00 00 00 | ................
212d8: 00 00 00 00 00 00 00 00 | ................
212e0: 00 00 00 00 00 00 00 00 | ................
212e8: 00 00 00 00 00 00 00 00 | ................
212f0: 00 00 00 00 00 00 00 00 | ................
212f8: 00 00 00 00 00 00 00 00 | ................
Segmentation fault
此外,任何使代码更简洁的提示都将非常有益。
你得到零的原因是你没有读取文件的内容,b
是用0x00
初始化的。您只需打开和关闭文件即可。
另一个问题(这可能是分段错误的原因(是b
的大小为 16 字节。在某些地方,您正在使用i % 16
但不是全部(在某些地方,您直接使用i
(。你绝对应该检查一下。