使用fgets从stdin(终端)读取一行,最大长度是1024?
Using fgets to read a line from stdin (terminal), the maximum length is 1024?
更新:
OSX 埃尔卡皮坦,Xcode 7.3
输入:
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890001
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFERSIZE 2048
int
main()
{
char buffer[BUFFERSIZE];
printf("Enter a message: \n");
while (fgets( buffer, BUFFERSIZE, stdin) != NULL)
{
printf("%s\n", buffer);
}
return 0;
}
在终端中编译并运行它。
./test
然后输入一行字符,长度为1024,不行;它无法打印缓冲区。当我输入 1023 个字符时,它将打印 1023 个字符。它可以打印 fgets
从本地打开文件中读取的超过 1024 个字符。
因此,对于来自终端的标准输入,最大长度为 1024,即使 <syslimits.h>
显示 ARG_MAX 是 (256 * 1024)。
我的代码有什么问题?
我有一些参考:
- How to read from stdin with fgets()?
- c-read-line-from-stdin-with-fgets-in-function
代码本身没有任何问题。问题是终端驱动程序在其缓冲区中有 1 KiB 的限制,因此您不能输入超过 1023 个字符加上一个换行符。大多数系统都有类似的限制。从历史上看,限制要小得多,比如 256 字节。
更新:
OSX 埃尔卡皮坦,Xcode 7.3
输入:
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890001
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFERSIZE 2048
int
main()
{
char buffer[BUFFERSIZE];
printf("Enter a message: \n");
while (fgets( buffer, BUFFERSIZE, stdin) != NULL)
{
printf("%s\n", buffer);
}
return 0;
}
在终端中编译并运行它。
./test
然后输入一行字符,长度为1024,不行;它无法打印缓冲区。当我输入 1023 个字符时,它将打印 1023 个字符。它可以打印 fgets
从本地打开文件中读取的超过 1024 个字符。
因此,对于来自终端的标准输入,最大长度为 1024,即使 <syslimits.h>
显示 ARG_MAX 是 (256 * 1024)。
我的代码有什么问题?
我有一些参考:
- How to read from stdin with fgets()?
- c-read-line-from-stdin-with-fgets-in-function
代码本身没有任何问题。问题是终端驱动程序在其缓冲区中有 1 KiB 的限制,因此您不能输入超过 1023 个字符加上一个换行符。大多数系统都有类似的限制。从历史上看,限制要小得多,比如 256 字节。