C编程中的Hex文件读取?

我有一个问题:

我正在编写的ac程序,其function与hex编辑器非常相似。 我想接收一个文件并查看其hex值。 例如,假设我的文本文件“helloworld.txt”只有“Hello World!”字样。 在其中,我想指定程序获取该文件,读取其hex值(同样该文件是一个简单的.txt文件,而不是二进制文件)并将其存储在一个数组中供以后使用。

这是我到目前为止:

#include  #include  int main() { char ch, file_name[25]; FILE *fp; printf("Enter the name of file you wish to see\n"); gets(file_name); fp = fopen(file_name,"rb"); // read binary mode if( fp == NULL ) //error checking { perror("Error while opening the file.\n"); exit(EXIT_FAILURE); } printf("The contents of %s file are :\n", file_name); while( ( ch = fgetc(fp) ) != EOF ) printf("%c",ch); fclose(fp); return 0; } 

正如您所看到的,这只是一个简单的文件,读取和打印内容。 但问题是,即使我处于“二进制”模式,它也不会给我文本文件的hex值。

我如何构造这个程序,以便它读取标准文本文件的hex值,并且(a)将它们放入一个数组中(我认为应该很简单,使用for循环)或者(b)将它们输出到屏幕?

我试着谷歌搜索,看看其他人是否正在尝试做同样的事情,但人们要么已经从二进制文件中读取,要么已经在文件中有hex值。 我在普通的.txt文件中有纯文本,但我想要下面的hex值(换句话说,是文件的一种hexdump)。 我希望我的问题以及我想要做的事情是有道理的。 如果您有任何疑问,请随时告诉我们! 在此先感谢您的帮助! 我真的很感激!

我不确定我是否正确理解了这个问题,但不会是那种简单使用的情况(对于选项B)

 printf("0X%02x ", ch); 
 #include  #include  #include  int main(int argc, char **argv) { FILE *fd; long filesize; char *buffer, *it; if ((fd = fopen(argv[1], "rb")) == NULL) { perror("Error opening file"); return EXIT_FAILURE; } fseek(fd, 0, SEEK_END); filesize = ftell(fd); rewind(fd); buffer = (char *) malloc(sizeof(char) * filesize+1); if (fread(buffer, sizeof(char), filesize, fd) != filesize) { fprintf(stderr, "Error reading file\n"); return EXIT_FAILURE; } buffer[filesize] = '\0'; for (it = buffer; *it != '\0'; it++) printf("%02X ", *it); free(buffer); return EXIT_SUCCESS; } 

你非常亲密。
注意2件事:

1-使用fgets()而不是gets()。
2- printf(“%02X”,ch);

 #include  #include  #include  int main() { char ch, file_name[25]; FILE *fp; printf("Enter the name of file you wish to see\n"); fgets(file_name, sizeof(file_name), stdin); file_name[strlen(file_name)-1] = 0x00; fp = fopen(file_name,"rb"); // read binary mode if( fp == NULL ) //error checking { perror("Error while opening the file.\n"); exit(EXIT_FAILURE); } printf("The contents of %s file are :\n", file_name); int i; while( ( ch = fgetc(fp) ) != EOF ) { printf("%02X ",ch); if( !(++i % 16) ) putc('\n', stdout); } fclose(fp); putc('\n', stdout); return 0; } 

当然这只是打印到stdout。 如果你想把结果放到一个数组中,你可以像sprintf()将你的输出放到一些缓冲区中。

输出示例:

 jrn@mint17 ~ $ ./a.out Enter the name of file you wish to see hex_for_so.c The contents of hex_for_so.c file are : 23 69 6E 63 6C 75 64 65 20 3C 73 74 64 69 6F 2E 68 3E 0A 23 69 6E 63 6C 75 64 65 20 3C 73 74 64 6C 69 62 2E 68 3E 0A 23 69 6E 63 6C 75 64 65 20 3C 73 74 72 69 6E 67 2E 68 3E 0A 0A 69 6E 74 20 6D 61 69 6E 28 29 0A 7B 0A 20 20 20 63 68 61 72 20 63 68 2C 20 66 69 6C 65 5F 6E 61 6D 65 5B 32 35 5D 3B 0A 20 20 20 46 49 4C 45 20 2A 66 70 3B 0A 0A 20 20 20 70 72 69 6E 74 66 28 22 45 6E 74 65 72 20 74 68 65 20 6E 61 6D 65 20 6F 66 20 66 69 6C 65 20 79 6F 75 20 77 69 73 68 20 74 6F 20 73 65 65 5C 6E 22 29 3B 0A 20 20 20 66 67 65 74 73 28 66 69 6C 65 5F 6E 61 6D 65 2C 20 73 69 7A 65 6F 66 28 66 69 6C 65 5F 6E 61 6D 65 29 2C 20 73 74 64 69 6E 29 3B 0A 20 20 20 66 69 6C 65 5F 6E 61 6D 65 5B 73 74 72 6C 65 6E 28 66 69 6C 65 5F 6E 61 6D 65 29 2D 31 5D 20 3D 20 30 78 30 30 3B 0A 0A 20 20 20 66 70 20 3D 20 66 6F 70 65 6E 28 66 69 6C 65 5F 6E 61 6D 65 2C 22 72 62 22 29 3B 20 2F 2F 20 72 65 61 64 20 62 69 6E 61 72 79 20 6D 6F 64 65 0A 0A 20 20 20 69 66 28 20 66 70 20 3D 3D 20 4E 55 4C 4C 20 29 20 2F 2F 65 72 72 6F 72 20 63 68 65 63 6B 69 6E 67 0A 20 20 20 7B 0A 20 20 20 20 20 20 70 65 72 72 6F 72 28 22 45 72 72 6F 72 20 77 68 69 6C 65 20 6F 70 65 6E 69 6E 67 20 74 68 65 20 66 69 6C 65 2E 5C 6E 22 29 3B 0A 20 20 20 20 20 20 65 78 69 74 28 45 58 49 54 5F 46 41 49 4C 55 52 45 29 3B 0A 20 20 20 7D 0A 0A 20 20 20 70 72 69 6E 74 66 28 22 54 68 65 20 63 6F 6E 74 65 6E 74 73 20 6F 66 20 25 73 20 66 69 6C 65 20 61 72 65 20 3A 5C 6E 22 2C 20 66 69 6C 65 5F 6E 61 6D 65 29 3B 0A 0A 20 20 20 69 6E 74 20 69 3B 0A 20 20 20 77 68 69 6C 65 28 20 28 20 63 68 20 3D 20 66 67 65 74 63 28 66 70 29 20 29 20 21 3D 20 45 4F 46 20 29 0A 20 20 20 7B 0A 20 20 20 20 20 20 70 72 69 6E 74 66 28 22 25 30 32 58 20 22 2C 63 68 29 3B 0A 09 20 20 69 66 28 20 21 28 2B 2B 69 20 25 20 31 36 29 20 29 20 70 75 74 63 28 27 5C 6E 27 2C 20 73 74 64 6F 75 74 29 3B 0A 20 20 20 7D 0A 20 20 20 66 63 6C 6F 73 65 28 66 70 29 3B 0A 20 20 20 72 65 74 75 72 6E 20 30 3B 0A 7D 0A