为什么写入文件时会出现神秘的段错误?

wob*_*aco 1 c io segmentation-fault

我希望我能提供更多信息,但我真的不知道这里发生了什么.此代码打开文件以进行附加或覆盖,具体取决于用户参数(默认情况下为appension).它可以达到用户输入的fgets,然后一旦输入输入,它就会段错误并转储核心.这很奇怪,因为在我实现参数之前(即它只是./a.out文件)它运行正常,所以我猜它与有关参数的新东西有关...

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

int printhelp(void);

int main(int argc, char *argv[])
{
  char input[256];
  int loopstat = 0;
  FILE *inputfile;

  if (argc < 2) /* Check argc for minimum 2 arguments (i.e. cw FILE) */
  {
    printf("ca: missing file operand\n");
    return 0;
  }
  else if (argc > 2) /* Check argc for more than 2 arguments (i.e. cw -o FILE) */
  {
    if (strncmp(argv[1], "-", 1) == 0) /* if first argument begins with "-", it must be an option, so descend into option checking */
    {
      if (strcmp(argv[1], "-a") == 0) /* If -a option is given, open for appending */
      {
        FILE *inputfile = fopen(argv[2], "a");
      }
      else if (strcmp(argv[1], "-o") == 0) /* If -o option is given, open for overwriting */
      {
        FILE *inputfile = fopen(argv[2], "w");
      }
      else if (strcmp(argv[1], "--help") == 0) /* If --help option is given, print help and quit */
      {
        printhelp();
        return 0;
      }
      else
      {
        printf("cw: invalid option\n"); /* If invalid option is given, print the fact and quit*/
        return 0;
      }
    }
  }
  else /* if argc is equal to 2 (i.e. "cw FILE" or "cw -o")...*/
  {
    if (strncmp(argv[1], "-", 1) == 0) /* Check if user has specified an option but no file (i.e. "cw -o") */
    {
      printf("cw: missing file operand\n"); /* If they have, print that no file is spec'd and quit */
      return 0;
    }
    else /* If not, it's a legit file with no other arguments (e.g. "cw FILE") so open it in append mode by default */
    {
      FILE *inputfile = fopen(argv[2], "a");
    }
  }

  /* Writing loop */
  printf("Enter input...\n");
  while (loopstat == 0) /* Get user input and write to file until they give exit command */
  {
    fgets(input, 256, stdin); /* Get user input */

    if (strcmp(input, ":x\n") == 0) /* If input == exit command, quit */
    {
      printf("co: exit received, terminating...\n");
      loopstat++;
    }
    else /* Write to file */
    {
      fprintf(inputfile, "%s", input);
    }
  }
  fclose(inputfile);
}

int printhelp(void) /* Print help on --help command */
{
  printf(
      "Usage: ca FILE\nContinuously append input to the FILE\nca does not currently support multiple file appension.\nReport bugs to scamp@lavabit.com\n");
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

PS抱歉,如果我弄乱了缩进,那么在这么多代码中的所有内容之前必须添加四个空格真的很混乱.

Dev*_*lus 5

在这里你要隐藏你的变量:

else  /* If not, it's a legit file with no other arguments (e.g. "cw FILE") so open it in append mode by default */
{
    FILE *inputfile = fopen(argv[2], "a");
}
Run Code Online (Sandbox Code Playgroud)

应该

else  /* If not, it's a legit file with no other arguments (e.g. "cw FILE") so open it in append mode by default */
{
    inputfile = fopen(argv[2], "a");
}
Run Code Online (Sandbox Code Playgroud)

你有几个像这样的实例,所以也删除那里的声明.