如何通过标准输入停止输入溢出
How to stop input overflowing via stdin
我的程序应该通过 stdin 接受输入并将字符串存储到大小为 SIZE
的数组中。
当我输入 7 个或更多值时出现问题;当程序在循环中运行时,超出缓冲区大小的值仍然被使用,不断地用 stdin 中剩下的内容填充 buf
(至少,我认为它是这样工作的)。
#define SIZE 8
char *readLine(char *buf, size_t size) {
printf("$ ");
fgets(buf, size, stdin);
return buf;
}
int main() {
char *buf = malloc(SIZE * sizeof(char));
for (;;) {
readLine(buf, SIZE);
printf("> %s", buf);
}
}
我的输出,分别测试 6、7 和 8 的输入大小:
$ abcdef
> abcdef
$ abcdefg
> abcdefg$ >
$ abcdefgh
> abcdefg$ > h
不明白的地方:
- 为什么第 7 个值使程序的输出看起来很奇怪?我认为第 7 个字符加上空字符可以正常工作?
- 为什么
buf
填充的值超出 SIZE
的大小(在本例中为 8)?
据此,有没有一个简单的change/addition我可以制作来实现我想制作的东西?
Why does the 7th value make the program's output appear strange? I thought the 7th character plus the null character would work correctly?
fgets
不会读取过多的输入(超出您提供的缓冲区大小,不包括空字节的一个字节 space)。因此,字符 h
留在输入流中,然后由您对 fgets
的下一次调用使用 - 即读取 h
和换行符 \n
。读取换行符后,fgets
终止读取进一步的输入。
Why does buf fill up with values beyond the size of SIZE (8 in this case)?
没有。如前所述,剩余的输入被读取,因此它给出了这样的外观。
From this, is there a simple change/addition I could make to achieve what I want to make?
您可以检查fgets
读取的缓冲区是否有换行符;如果不是,则读取并忽略输入流中剩余的字节。
你可以这样做:
void discard(const char *buf)
{
if(strchr(buf, '\n') == NULL) {
/* No newline found, so read & discard everything.
while(getchar() != '\n');
}
}
char *readLine(char *buf, size_t size) {
printf("$ ");
if (fgets(buf, size, stdin) != NULL) {
discard(buf);
}
return buf;
}
我的程序应该通过 stdin 接受输入并将字符串存储到大小为 SIZE
的数组中。
当我输入 7 个或更多值时出现问题;当程序在循环中运行时,超出缓冲区大小的值仍然被使用,不断地用 stdin 中剩下的内容填充 buf
(至少,我认为它是这样工作的)。
#define SIZE 8
char *readLine(char *buf, size_t size) {
printf("$ ");
fgets(buf, size, stdin);
return buf;
}
int main() {
char *buf = malloc(SIZE * sizeof(char));
for (;;) {
readLine(buf, SIZE);
printf("> %s", buf);
}
}
我的输出,分别测试 6、7 和 8 的输入大小:
$ abcdef
> abcdef
$ abcdefg
> abcdefg$ >
$ abcdefgh
> abcdefg$ > h
不明白的地方:
- 为什么第 7 个值使程序的输出看起来很奇怪?我认为第 7 个字符加上空字符可以正常工作?
- 为什么
buf
填充的值超出SIZE
的大小(在本例中为 8)?
据此,有没有一个简单的change/addition我可以制作来实现我想制作的东西?
Why does the 7th value make the program's output appear strange? I thought the 7th character plus the null character would work correctly?
fgets
不会读取过多的输入(超出您提供的缓冲区大小,不包括空字节的一个字节 space)。因此,字符 h
留在输入流中,然后由您对 fgets
的下一次调用使用 - 即读取 h
和换行符 \n
。读取换行符后,fgets
终止读取进一步的输入。
Why does buf fill up with values beyond the size of SIZE (8 in this case)?
没有。如前所述,剩余的输入被读取,因此它给出了这样的外观。
From this, is there a simple change/addition I could make to achieve what I want to make?
您可以检查fgets
读取的缓冲区是否有换行符;如果不是,则读取并忽略输入流中剩余的字节。
你可以这样做:
void discard(const char *buf)
{
if(strchr(buf, '\n') == NULL) {
/* No newline found, so read & discard everything.
while(getchar() != '\n');
}
}
char *readLine(char *buf, size_t size) {
printf("$ ");
if (fgets(buf, size, stdin) != NULL) {
discard(buf);
}
return buf;
}