fgets被跳过 [英] fgets is getting skipped

查看:96
本文介绍了fgets被跳过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有个小程序,我想先问一个选项然后再问一个文件名.

I have little program where I want to ask for an option and then for a filename.

  //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

问题是我的fgets被跳过了,即使在gdb中,gdb在 fgets for inspect输入之后显示值"\ n \ 000]-",当 action = 1

The problem is that my fgets is getting skipped, even in gdb, gdb shows after the fgets for inspect input the value "\n\000]--" when action = 1

这是控制台输出:

######################
# 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

推荐答案

fget()不会被跳过,而是使用 getchar()从上一个输入中读取剩余的换行符.

fget() is not getting skipped but reads the left over new-line from the previous input using getchar().

这完全是关于完整错误检查和合理的日志记录:

It is all about complete error checking and sane logging:

#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;
  }
}

这篇关于fgets被跳过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆