为什么 system("cls") 在 Windows 上对我不起作用?

问题描述 投票:0回答:1

我一直在尝试让屏幕每秒刷新一次,但我不明白为什么它不起作用。 倒计时有效,但我只想显示最后一个“printf”并删除它之前的所有其他“printf”。 顺便说一句,我在 Windows 上,而不是在 Linux 或 Mac 上

这就是我尝试做的:

#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <windows.h>

int main() {
    int décompte;
    décompte = 120;
    time_t debut, maintenant;
    int tempsrestant;

    time(&debut);
    while(1){
        time(&maintenant);
        tempsrestant = décompte - (maintenant - debut);
        if(tempsrestant == 0){
            printf("vous perdez une de vos trois vies\n"),
            sleep(2);
            break;
        }
        else if(tempsrestant>0){
            printf(" tempsrestant : %d sec",tempsrestant);
            sleep(1);
            system("cls");
        }
    }

    return 0;
}
c windows console refresh
1个回答
0
投票

您同时包含了 UNIX 特定的头文件 (unistd.h) 和 Windows 特定的头文件 (windows.h),所以我猜测您正在 UNIX 模拟环境中操作,例如 Cygwin 提供的环境、MSYS 和 MSYS2。

作为 Unix 仿真,它们使用

sh
而不是
cmd
来执行传递给
system
的 shell 命令。由于
cls
是内置的
cmd
,因此这不起作用。执行
clear
可能会。

© www.soinside.com 2019 - 2024. All rights reserved.