使用EOF在while循环中停止scanf

时间:2019-06-22 01:16:08

标签: c scanf

我需要获取浮点输入,并且仅在用户按下Ctrl + Z时才停止。

但是,在这样的循环中,我的程序需要按顺序3 crtl + Z。有谁知道如何用EOF阻止它?

这是我的代码:

我有while循环条件分析输入是否为浮点数。

int capacity = 5;

double* numSet = (double*)malloc(capacity * sizeof(*numSet));
if (numSet == NULL)
    return NULL;

double number;
char ch = 0;


while (fscanf_s(stdin,"%lf", &number) == 1)
{


    numSet[size] = number;
    size++;

    if (size == capacity)
    {
        double* numSetDouble = realloc(numSet, (capacity *= 2) * sizeof(*numSetDouble));

        if (numSetDouble == NULL)
        {
            free(numSet);
            return NULL;
        }
        numSet = numSetDouble;
    }
}

return numSet;

我需要在用户输入ctrl ^ Z时停止循环

0 个答案:

没有答案