#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 255
void hexDump (char *desc, void *addr, int len) {
int i;
unsigned char buffLine[17];
unsigned char *pc = (unsigned char*)addr;
if (desc != NULL){
printf ("%s:\n", desc);
}
for (i = 0; i < len; i++) {
if ((i % 16) == 0) {
if (i != 0)
printf (" %s\n", buffLine);
// if (buffLine[i]== '\0') break;
if (pc[i] == 0x00) exit(0);
// Prints the ADDRESS
printf (" %07x ", i);
}
// Prints the HEXCODES that represent each chars.
printf ("%02x", pc[i]);
if ((i % 2) == 1)
printf (" ");
if ((pc[i] < 0x20) || (pc[i] > 0x7e)){
buffLine[i % 16] = '.';
}
else{
buffLine[i % 16] = pc[i];
}
buffLine[(i % 16) + 1] = '\0'; //Clears the next array buffLine
}
while ((i % 16) != 0) {
printf (" ");
i++;
}
printf (" %s\n", buffLine);
}
//----------------------------------------------
int main()
{
FILE *ptr_file;
char buff[SIZE];
ptr_file =fopen("input.txt","r");
if (!ptr_file){
printf("returning 1");
return 1;
}
memset(buff, '\0', sizeof(buff));
fgets(buff,SIZE, ptr_file);
hexDump ("buff", &buff, sizeof (buff));
fclose(ptr_file);
return 0;
}
//*/
對不起,這個我是新的C,所以它有點凌亂,現在,我已經刪除的部分,使臨時代碼,等等 所以反正這是通過讀取字符從input.txt輸出並通過在右側打印十六進制表示和Acii表示形式輸出。因爲,打印一個「」字符(點/句號,即十六進制值2E):形成非打印字符字節hexdump都計劃
0003540: 0504 0675 6e73 6967 6e65 6420 6368 6172 ...unsigned char
0003550: 0008 0107 0000 0131 0675 6e73 6967 6e65 .......1.unsigne
,但我的程序不打印任何超出了‘新線’文本。 例如: 在我input.txt中,我有:
This is line 1 so this will be longer than usual
line 2 is this
this is the last line
閱讀我的程序後,它只輸出是這樣的:
0000000 5468 6973 2069 7320 6c69 6e65 2031 2073 This is line 1 s
0000010 6f20 7468 6973 2077 696c 6c20 6265 206c o this will be l
0000020 6f6e 6765 7220 7468 616e 2075 7375 616c onger than usual
0000030 0a00 0000 0000 0000 0000 0000 0000 0000 ................
但由於某些原因,它不會打印和十六進制第二行,第三行....
您或者需要在循環中調用'fgets',一次讀取一行或使用'fread'來讀取固定大小的塊。 – 2014-09-22 08:18:02