问题
I had a question:
I am writing a c program that functions much like a hex editor. I want to take in a file and view its hexadecimal values. For example, say I had the text file "helloworld.txt" that simply had the words "Hello World!" in it, I want to specify to the program to take that file, read its hexidecimal values (again the file is a simple .txt file and not a binary file) and store it in an array for later.
Here is what I have so far:
#include <stdio.h>
#include <stdlib.h>
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;
}
As you can see this is really just a simple file in, read and print contents. However, the problem is that even though I'm in "read binary" mode, it won't give me the hexadecimal values of the text file.
How can I structure this program so that it reads the hexadecimal values of a standard text file and either (a) puts them in an array (which I think should be simple enough with a for loop) or (b) prints them out to the screen?
I tried googling to see if others were trying to do the same thing, but people were either already reading from a binary file or already had hexadecimal values in the file. I have plain text in a regular .txt file, but I want the hexadecimal values underneath (in other words a sort of hexdump of the file). I hope my question and what I'm trying to do makes sense. If you have any questions please feel free to let me know! Thanks in advance for all your help! I really do appreciate it!
回答1:
Not sure if I understand the question correctly, but wouldn't that be the case (for option B) of simply using
printf("0X%02x ", ch);
回答2:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}
回答3:
You are so very close.
Notice 2 things:
1- Use of fgets() instead of gets().
2- printf("%02X ",ch);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}
Of course this just prints to stdout. If you want to put the result into
an array you could something like sprintf()
to put your output into some buffer(s).
Example output:
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
来源:https://stackoverflow.com/questions/22057846/hex-file-reading-in-c-programming