【问题标题】:Number Pad [Enter] not \n in C?数字键盘 [Enter] 不是 C 中的 \n?
【发布时间】:2011-08-30 18:28:00
【问题描述】:

我正在为大学作业编写一个小型 C 程序,我注意到我的代码中有一个奇怪的错误。我通常使用带短键盘的 iMac,但它的电池没电了,所以我插入了带数字键盘的标准 USB 键盘。

奇怪的是,如果我在数字键盘上按 [Enter],它似乎会执行常规 [Enter} 键的作用,但是我试图在我为读取键盘输入而创建的 stdin 函数,当我使用数字键盘的 [Enter] 键时不起作用。

什么?

这是我读取用户输入的函数:

/* This is my implementation of a stdin "scanner" function which reads
 * on a per character basis until the the termination signals are found
 * and indescriminately discarding all characters in the input in excess
 * of the supplied (limit) parameter.  Eliminates the problem of 'left-over'
 * characters 'polluting' future stdin reads.
 */
int readStdin(int limit, char *buffer) 
{
   char c;
   int i = 0;
   int read = FALSE;
   while ((c = myfgetc(stdin)) != '\n' && c != '\0') {
      /* if the input string buffer has already reached it maximum
       limit, then abandon any other excess characters. */
      if (i <= limit) {
         *(buffer + i) = c;
         i++;
         read = TRUE;
      }
   }
   /* clear the remaining elements of the input buffer with a null character. */
   for (i = i; i < strlen(buffer); i++) {
      *(buffer + i) = '\0';
   }
   return read;
}

/* This function used to wrap the standard fgetc so that I can inject programmable
 * values into the stream to test my readStdin functions.
 */
int myfgetc (FILE *fin) {
   if (fakeStdIn == NULL || *fakeStdIn == '\0')
      return fgetc (fin);
   return *fakeStdIn++;
}

注意:myfgetc 和随后的*fakeStdIn 是我可以单元测试我的代码并将项目“注入”到标准输入流中的一种方式,正如有人在这个问题上建议的那样:How do I write a testing function for another function that uses stdin input?

【问题讨论】:

  • 为什么不添加调试 printf 语句来查看字符是什么?我的猜测是它是 0x03。
  • 请注意,您的 for 循环清除缓冲区非常尴尬; i=i 值得一笑(您可以将任何表达式留空:for(;;) 是无限循环的有效语法)但i &lt; strlen(buffer) 假设在分配给buffer 的空间中已经有一个'\0'。那可能不是真的。之后通过limit 参数清除空间,或者通过limit 参数将读取之前的空间清零,但不要使用strlen(buffer) 来确定何时终止循环。跨度>
  • 所以我这样做了,结果没有一个数字键盘键实际上进入标准输入流。它们在控制台中工作,但是当我将数字键盘和非数字键盘字符混合在一起时,char* 中只会出现非数字键盘字符。
  • 取决于终端和窗口环境;在我的 X11 下的 Linux 上,returnkb_enter 键都生成 '\n' 字符(十进制 10),但 return 生成 X11 键符 Returnkp_enter 生成 KP_Enter。在我的环境中,两者都映射到'\n'。你的,显然不同。 :)
  • @sarnold - 我正在运行 vanilla OSX 10.6 - 数字键盘键似乎没有映射到任何东西。至少不是标准输入。

标签: c macos keyboard stdin


【解决方案1】:

所以事实证明这是 Mac OSX 的东西。我和其他 Mac 用户谈过,他们也有同样的问题。从来没有找到一个修复,因为一个可能根本不存在。这个问题不会出现在 Solaris 机器上,因为这是运行代码的操作系统,我想这并不重要。

我将自己回答这个问题,它只是那些 OSX“怪癖”之一,并完成它。

【讨论】:

    【解决方案2】:

    很可能在 Mac 上,您收到的是 \r\n,而不仅仅是 \n

    【讨论】:

      【解决方案3】:

      这个小测试你会得到什么输出?

      #include <stdio.h>
      int main(int argc, char* argv[]) {
          int c;
          while((c=getchar()) != EOF) {
              printf("%d\n", c);
          }
          return 0;
      }
      

      【讨论】:

      • 它不输出任何东西(每次我按小键盘上的回车键时都会换行)。当我按常规回车键时,它会输出“10”。
      • 我的普通 Mac 键盘电池没电,而我使用的 USB 键盘没有功能键。
      • 噢!愚蠢的我,你在问题的第一句话中解释了这一点!我已经删除了我的愚蠢评论,以免混淆其他人!
      猜你喜欢
      • 2019-11-19
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      • 2013-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      相关资源
      最近更新 更多