首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >fgets被跳过了

fgets被跳过了
EN

Stack Overflow用户
提问于 2018-04-14 10:24:54
回答 1查看 479关注 0票数 1

我有一个小程序,我想要求一个选项,然后一个文件名。

代码语言:javascript
复制
  //Some code before
   printf("######################\n");
   printf("# 1. Register a file #\n");
   printf("# 2. Get global list #\n");
   printf("# 3. Download a file #\n");
   printf("# 4. Quit / Exit     #\n");
   printf("######################\n");
   printf("Enter decision: ");
      fflush(stdin);
   action = getchar();
   action -= '0';
   sprintf(input, "[%d]--", action);
   switch (action)
   {
    case 1:
     printf("Enter file name: ");
        fflush(stdin);
     fgets(input+strlen(input), LINE_LEN, stdin);
     input[strlen(input)] = (input[strlen(input)] == '\n') ? 0 : input[strlen(input)];
     if(write(sock, input, sizeof(input)) == -1) perror("Clienthandler Write 3");
    break;
//some code after

问题是,我的fget被跳过了,即使在gdb中,gdb在fgets for inspect input后面显示action = 1时的值"\n\000]--"

这是控制台输出:

代码语言:javascript
复制
######################
# 1. Register a file #
# 2. Get global list #
# 3. Download a file #
# 4. Quit / Exit     #
######################
Enter decision: 1
Enter file name: ######################
# 1. Register a file #
# 2. Get global list #
# 3. Download a file #
# 4. Quit / Exit     #
######################
Enter decision: ^C
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-15 10:21:33

fget()没有被跳过,而是使用getchar()从前面的输入读取新行的左边。

这一切都是关于完全错误检查和理智的日志记录:

代码语言:javascript
复制
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


#define LINE_LEN (1024)


int main(void)
{
  int action = 0;
  char input[LINE_LEN] = {0};
  size_t len = 0;

  printf("enter decision: ");
  {
    int c = fgetc(stdin); /* equivalent to getchar() */
    if (EOF == c)
    {
      putc('\n');

      if (ferror(stdin))
      {
        perror("fgetc(stdin) failed");
      }
      else
      {
        fprintf(stderr, "read EOF, aborting ...\n");
      }

      exit(EXIT_FAILURE);
    }

    action = c;
  }

  /* read left over from previous input: */
  {
    int c = fgetc(stdin);
    if (EOF == c)
    {
      putc('\n');

      if (ferror(stdin))
      {
        perror("fgetc(stdin) failed");
        exit(EXIT_FAILURE);
      }
    }
    else if ('\n' != c)
    {
      fprintf(stderr, "read unexpected input (%d), aborting ...\n", c);
      exit(EXIT_FAILURE);
    }
  }

  len = strlen(input);
  if (LINE_LEN <= len + 1) 
  {
    fprintf(stderr, "input buffer full, aborting ...\n");
    exit(EXIT_FAILURE);
  }

  switch(action - '0')
  {
    case 1:
      printf("enter file name: ");
      if (NULL == fgets(input + len, LINE_LEN - len, stdin))
      {
        putc('\n');

        if (ferror(stdin))
        {
          perror("fgets() failed");
        }
        else
        {
          fprintf(stderr, "missing input, aborting ...\n");
        }

        exit(EXIT_FAILURE);
      }

      (input + len)[strcspn(input + len, "\n")] = '\0'; /* cut off new-line, if any. */

      printf("read file name '%s'\n", input + len);

      break;

    default:
      fprintf(stderr, "unknown action (%d), aborting ...\n", action);
      exit(EXIT_FAILURE);

      break;
  }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49830399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档