2011-01-07 85 views
1
#include <stdio.h> 
#include<stdio.h> 
#include <ctype.h> 

    /** 
    * Display the contents of a stream as hexadecimal bytes and text. 
    * @param file pointer to a binary stream 
    * @return the number of bytes in the file read 
    */ 
    size_t hexdump(FILE *file,FILE *fp,FILE *fp1) 
    { 
     unsigned char data[16]; 
     size_t i, j, size, count = 0; 
     /* 
     * Heading 
     */ 
     fputs(" ", stdout); /* skip address area */ 
     for (i = 0; i < sizeof data/sizeof *data; ++i) 
     { 
      printf("+%lX ", (long unsigned)i); 
     } 
     puts("Text"); 
     /* 
     * Body 
     */ 
     do { 
      /* Read some data. */ 
      size = fread(data, sizeof *data, sizeof data/sizeof *data, file); 
      if (size) 
      { 
       /* Print the base address. */ 
       printf("%08lX ", (long unsigned)count); 
       fprintf(fp,"%08lX",(long unsigned)count); 
       count += size; /* adjust the base address */ 
       /* Print the characters' hex values. */ 
       for (i = 0; i < size; ++i) 
       { 
        printf("%02X ", data[i]); 
        fprintf(fp1,"%02X",data[i]); 
       } 
       /* Pad the hex values area if necessary. */ 
       for (++i; i <= sizeof data/sizeof *data; ++i) 
       { 
        fputs(" ", stdout); 
       } 
       /* Print the characters (use '.' for non-printing characters). */ 
       /*  for (j = 0; j < size; j++) 
         { 
         putchar(isprint(data[j]) ? data[j] : '.'); 
         }*/ 
       putchar('\n'); 
      } 
     } while (size == sizeof data/sizeof *data); /* Break on partial count. */ 
     return count; 
    } 

    int main(void) 
    { 
     char filename[20] ;/* may not work on everywhere */ 
     char filename1[20]; 
     char filename2[20]; 
     printf("enter the filename\n"); 
     scanf("%s",filename); 
     printf("enter the filename to store address\n"); 
     scanf("%s",filename1); 
     printf("enter the filename to store data\n"); 
     scanf("%s",filename2); 
     FILE *file = fopen(filename, "rb"); 
     FILE *fp= fopen(filename1,"wb"); 
     FILE *fp1=fopen(filename2,"rb"); 
     if (file != NULL) 
     { 
      printf("%lu bytes\n", (long unsigned)hexdump(file,fp,fp1)); 
      fclose(file); 
      fclose(fp); 
      fclose(fp1); 
     } 
     else 
     { 
      perror(filename); 
     } 
     return 0; 
    } 

执行上面的代码时,我得到了段错误。这里的输出:数据处理代码段错误

+0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F Text 
Segmentation fault 

这是什么原因?

+0

你想用sizeof data/sizeof * data实例来完成什么?特别是在fread和for循环中。 – JimR 2011-01-07 10:02:09

+0

您的问题可能会受益于一点编辑,因为代码块分成两部分,并且在开始时有很多空白行。 – 2011-01-07 10:03:30

+0

这功课吗? – AlastairG 2011-01-07 10:04:32

回答

1
FILE *fp1=fopen(filename2,"rb"); 

您打开fp1进行阅读。然而你写信给它。将其更改为

FILE *fp1=fopen(filename2,"wb"); 
0

首先,它是平常的sizeof作为sizeof(a)而非sizeof a使用。事实上我没有意识到你甚至可以使用没有括号的sizeof。其次,我不知道sizeof的优先级,所以不知道sizeof data/sizeof *data给出了什么结果。改为尝试做sizeof(data)/sizeof(*data)。它可能被解释为sizeof(data/sizeof(*data))。我怀疑它不是,但无论如何它使它更具可读性并且是很好的做法。

最后,你确定fp是否为非NULL?你不检查你的输入参数(总是这样做的一个好主意),所以不能保证它不是NULL,如果它是NULL,你会得到一个段错误,在你似乎得到的地方。