sscanf()通过更改for循环的索引值导致无限循环

时间:2020-05-29 18:16:49

标签: c linux gcc

功能:

void input_bytes(unsigned char bytes[], int num_bytes)
{
    const int CHAR_PER_BYTE = 2; /* expects hex input */
    const int STRING_SIZE = num_bytes * CHAR_PER_BYTE;

    char input_string[STRING_SIZE + 1];  /* + 1 for null char */
    int ind;
    unsigned char byte;

    fgets(input_string, STRING_SIZE + 1, stdin);

    printf("after fgets\n");

    for (ind = 0; ind < num_bytes; ind++)
    {
        printf("top of loop, ind: %d\n", ind);

        sscanf(input_string, "%2x", &byte);
        bytes[ind] = byte;

        printf("bottom of loop, ind: %d\n", ind);
    }

    printf("\n\n");
}

驱动程序:

int main()
{
    unsigned char bytes[5];

    input_bytes(bytes, 5);
}

输出:

after fgets
top of loop, ind: 0
bottom of loop, ind: 0
top of loop, ind: 1
bottom of loop, ind: 0
top of loop, ind: 1
bottom of loop, ind: 0
top of loop, ind: 1
bottom of loop, ind: 0
top of loop, ind: 1
bottom of loop, ind: 0
top of loop, ind: 1
bottom of loop, ind: 0
top of loop, ind: 1
bottom of loop, ind: 0
top of loop, ind: 1
bottom of loop, ind: 0
top of loop, ind: 1
bottom of loop, ind: 0
...

使用gcc driver.c -o driver进行编译。

出于好奇,我将ind++放入循环中,它的顶部是2,底部是1。如果我取出sscanf(),它将终止。

1 个答案:

答案 0 :(得分:2)

%x格式相对应的变量必须为int,而不是char。使用错误的类型会导致未定义的行为;在您的程序中,它是将0写入ind,因此循环永远不会停止。

size的{​​{1}}参数应该是缓冲区的大小,不加1。fgets()将减去1以得到它可以读取的字符数,然后再返回null。您应该在fgets()的大小上加上1,而不是input_string的参数。

fgets()在返回的输入中也包含换行符,您还需要为fgets()添加空间。

如果要转换输入中的所有字节,则每次循环时都需要从input_string中的另一个索引进行扫描。

input_string

由于您实际上并未在void input_bytes(unsigned char bytes[], int num_bytes) { const int CHAR_PER_BYTE = 2; /* expects hex input */ const int STRING_SIZE = num_bytes * CHAR_PER_BYTE + 2; // + 2 for newline and null byte char input_string[STRING_SIZE]; int ind; unsigned int byte; fgets(input_string, STRING_SIZE, stdin); /* + 1 for null char */ printf("after fgets\n"); for (ind = 0; ind < num_bytes; ind++) { printf("top of loop, ind: %d\n", ind); sscanf(&input_string[2*ind], "%2x", &byte); bytes[ind] = byte; printf("bottom of loop, ind: %d\n", ind); } printf("\n\n"); } 上执行任何字符串操作。您可以精确地将其设置为input_string个字符,而无需为换行符和null添加空格。不用使用num_bytes * CHAR_PER_BYTE,而是使用fgets()来精确读取所需的字符数:

fread()