简体   繁体   English

C程序停止接受1023个字符的输入

[英]C program stops accepting input at 1023 characters

I'm teaching myself C using K&R. 我正在使用K&R教自己C. Exercise 1-16 asks me to refactor some provided code to give the length of "arbitrarily long input lines". 练习1-16要求我重构一些提供的代码,以给出“任意长输入行”的长度。

Whilst working on the problem I found that my terminal ceases to accept input after 1023 characters; 在解决这个问题时,我发现我的终端在1023个字符后不再接受输入; a very suspicious number I'm sure you'll agree!! 一个非常可疑的数字,我相信你会同意的! I have tested on Mac OS X and OpenBSD and see the same behaviour. 我已经在Mac OS X和OpenBSD上测试过并看到了相同的行为。 The program hasn't stopped responding because typing backspace and submitting the input works correctly. 程序没有停止响应,因为键入退格并提交输入正常工作。

I couldn't figure out how to debug this with gdb because the problem occurs during data entry, not after submission when stepping through with gdb. 我无法弄清楚如何使用gdb调试此问题,因为问题发生在数据输入期间,而不是在提交时使用gdb。

I could see no reference to a limit in the getchar or bash manpages, and indeed it seems very little input anyway. 我看不到在getchar或bash联机帮助页中没有引用限制,事实上它似乎很少输入。

I reduced the problem to the following and see the same behaviour. 我将问题减少到以下并看到相同的行为。

#include <stdio.h>

main()
{
  int c,i=0;

  while ((c=getchar()) != EOF && c!='\n')
    ++i;
  printf("%d\n",i);

  return 0;
}

Could people please explain: 有人可以解释一下:

  1. Why this is happening 为什么会这样
  2. How I might debug this kind of issue myself 我如何自己调试这类问题

Many thanks. 非常感谢。

As per the comments on my question, it would appear to be a terminal limitation. 根据我的问题的评论,它似乎是一个终端限制。 Piping a file into the program works as expected. 将文件管道插入程序按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM