c - 数据处理代码中的段错误

标签 c segmentation-fault

#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

这可能是什么原因?

最佳答案

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

您打开 fp1 进行阅读。然而你写信给它。改成

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

关于c - 数据处理代码中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4624297/

相关文章:

c - 如何避免使用 "extern"在 C 中的多个文件之间共享一个实例?

macos - mac 上的 kannel 1.5 addons sqlbox 连接到 Postgresql 9.3.5,出现 "Segmentation fault: 11"错误

mysql - 提交查询时出现段错误

c - 调用正确的函数时发生段错误

c - C中队列的段错误

c - 如何在 C 中在终端上显示输出并同时保存到文件?

c - 伪终端段错误

c - 从标准输入中读取一个自然数 n。找到小于或等于 n 的最大完美平方

c - 使用 dlopen 访问 CMake 生成的动态库

android - C++ 段错误输出