如果输入的长度大于2,为什么此代码将多次跳过循环?

似乎这与缓冲区溢出有关。但是我可以在循环结束时清除输入缓冲区吗?

#include <stdio.h>

int main(void)
{
    char in[2];
    while (in[0] != 'q') {
        puts("Enter: ");
        fgets(in, 3, stdin);
    }
    return 0;
}


我想请用户在每个循环中输入一些字符串。

最佳答案

首先,您的缓冲区不足以容纳2个字符串。 C中的字符串需要附加一个附加的'\ 0'来标记结尾。其次,当输入字符串的长度大于传递给它的缓冲区时,fgets会将字符保留在输入缓冲区中。您需要先消耗这些字符,然后才能再次调用fgets

这是一个类似于fgets的函数,因为它将仅读取buffer_len - 1字符。并且它将消耗所有字符,直到换行符或EOF。

int my_gets(char *buffer, size_t buffer_len)
{
    // Clear buffer to ensure the string will be null terminated
    memset(buffer, 0, buffer_len);

    int c;
    int bytes_read = 0;
    // Read one char at a time until EOF or newline
    while (EOF != (c = fgetc(stdin)) && '\n' != c) {
        // Only add to buffer if within size limit
        if (bytes_read < buffer_len - 1) {
            buffer[bytes_read++] = (char)c;
        }
    }
    return bytes_read;
}

int main(void)
{
    char in[3];  // Large enough for a 2 char string

    // Re-arranged to do/while
    do {
        puts("Enter: ");
        my_gets(in, sizeof(in));  // sizeof(in) == 3
    } while (in[0] != 'q');
    return 0;
}

关于c - 如何处理C中的缓冲区溢出?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55794557/

10-12 23:51
查看更多