EOF在c循环时阻塞

-1 c eof

我正在尝试在c中创建一个代码,只需用cmd命令"Wmic logicaldisk get"在txt文件中写入磁盘c信息,但我只需要数字(大小为4294931).所以我选择这个输出并将其放入一个txt文件中以获得输入中的唯一数字.(我知道这很奇怪).

这是完整的代码:

#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <ctype.h>

int main()
{
    system("wmic logicaldisk get size> test.txt");

    unsigned char symb;
    FILE *FileIn;
    FileIn = fopen("test.txt","rt"); 
    int getc(FILE *stream);
    while (( symb = getc(FileIn)) !=EOF) 
    {
        if( isdigit(symb))
        {
            printf("%C", symb);
        }
    }
    printf("test"); //for debug
}
Run Code Online (Sandbox Code Playgroud)

代码工作,但不能退出循环,而它正确打印的数字,但下一个命令不执行(所以不执行pritnf测试).

Som*_*ude 5

您的代码中有三件事情是错误的.

  1. 你重新宣布原型getc.您不应该这样做,因为您的声明可能与官方标准声明不同.

  2. getc函数返回一个int.那是因为EOF是一个int常数,有价值-1.并且((unsigned char) -1) != -1.这是因为该unsigned char-1实际上255并且不等于-1.与getc(或任何类似函数)一起使用的变量必须是a int.

  3. printf格式说明"%C"(用大写C)是不是一个标准格式说明.它是Microsoft Visual C++扩展,适用于类型的宽字符wchar_t.由于您的变量symb不是与格式说明符匹配的正确类型,因此您将具有未定义的行为.对于像你这样的狭隘角色你应该使用小写字母c.