分段故障(核心转储)无法访问内存

时间:2016-12-24 21:25:41

标签: c

我正在尝试在linux中学习c程序。我正在使用fopen()函数打开文本文件。我的程序正在抛出Segmentation fault(核心转储)。这是代码:

/* This program is written to demonstrate the uses for 
   getopt_long function to define option argument in linusx system*/

#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* The name of this program. */
const char* program_name;

FILE *file, *file1;
char sentence[1035];


/* Prints Usage information for this program to STREAM(Typically 
   stdout or stderr), and exit the program with EXIT_CODE. Does not
   return. */

void print_usage(FILE* stream, int exit_code)
{
  fprintf (stream," Usage: %s options [ inputfile...]\n", program_name);
  fprintf (stream,
       "  -h  --help               Display this usage information.\n"
       "  -o  --output filename    write output to file \n"
       "  -v  --verbose            Print verbose messages.\n");
  exit (exit_code);
}

/* Main program entry point . ARDC contains number of argument list
   elements, ARGV is an array of pointers to them.*/

int main (int argc , char* argv[])
{
  int next_option;
  const char filename[1000];
  /* A string listing valid short option letters. */
  const char* const short_options = "ho:v";
  /*An array describing valid long options.  */
  const struct option *long_options = {
    { "help",     0, NULL,  'h'},
    { "output",   1, NULL,  'o'},
    { "verbose",  0, NULL,  'v'},
    {  NULL,      0, NULL,   0}   //Required at end of array
  };

  /* The name of the file to reciev program out, or NULL for 
     standard output.*/

  const char* output_filename = NULL;
  /* Where to dispaly verbose messagae.*/
  int verbose = 0;

  /*Remember the name of the program, to incorporate in messages.
    The name is stored in argv[0]. */
  program_name = argv[0];


  do {
    next_option = getopt_long (argc, argv, short_options,
                   long_options, NULL);

    switch (next_option)
      {
      case 'h': /* -h or --help */
    /* User has requested usage information. Print it to standard 
       output, and exit with exit code zero (normal terminatio ). */
    print_usage (stdout, 0);

      case 'o':  /* -o or --output */
    /*  This option takes an argument, the name of the output file . */
    output_filename = optarg;
    file = popen("/bin/ls /etc/", "r");

    if (file == NULL){
      printf("Failed to run command\n");
      exit(1);
    }

    strcpy(filename, "/home/biki/Desktop/C_Prog/program2/");
    strcat(filename,output_filename);
    file1 = fopen(filename, 'w+');

    if (file1 == NULL)
    {
        printf("Error while opening text.txt");
        exit(1);
    }

    while(fgets(sentence, sizeof(sentence)-1, file) != NULL)
      {

        fprintf(file1, "%s", sentence);
      }

    /*close file*/
    pclose(file1);
    pclose(file);

      case 'v':   /* -v or --verbose */
    /* Print usage information to standard error, and exit with exit
       code one (indication abnormal termination). */
    print_usage (stderr, 1);

      case -1:  /* done with options. */
    break;

      default : /*Something else. unexpected. */
    abort ();
      }
  }
  while (next_option != -1);

  if (verbose) {
    int i;
    for (i = optind; i < argc; ++i)
      printf ("Argument: %s\n", argv[i]);
  }

  /* the main program goes here  */
  return 0;
}

这是gdb

Program received signal SIGSEGV, Segmentation fault.
_IO_new_file_fopen (fp=fp@entry=0x555555757120, 
    filename=filename@entry=0x7fffffffdb60 "/home/biki/Desktop/C_Prog/program2/text.txt", 
    mode=mode@entry=0x772b <error: Cannot access memory at address 0x772b>, 
    is32not64=is32not64@entry=1) at fileops.c:270
270 fileops.c: No such file or directory.
(gdb) backtrace
#0  _IO_new_file_fopen (fp=fp@entry=0x555555757120, 
    filename=filename@entry=0x7fffffffdb60 "/home/biki/Desktop/C_Prog/program2/text.txt", 
    mode=mode@entry=0x772b <error: Cannot access memory at address 0x772b>, 
    is32not64=is32not64@entry=1) at fileops.c:270
#1  0x00007ffff7a7ff84 in __fopen_internal (
    filename=0x7fffffffdb60 "/home/biki/Desktop/C_Prog/program2/text.txt", 
    mode=0x772b <error: Cannot access memory at address 0x772b>, is32=1)
    at iofopen.c:86
#2  0x0000555555554ced in main (argc=3, argv=0x7fffffffe038)
    at getopt_long.c:82

谁能告诉我这是什么问题?

0 个答案:

没有答案