提问人:Rick Mortie 提问时间:11/1/2023 更新时间:11/1/2023 访问量:68
为什么 system(“cls”) 在 Windows 上对我不起作用?
Why doesn't system("cls") work for me on windows?
问:
我一直在尝试让我的屏幕每秒刷新一次,但我不明白为什么它不起作用。 倒计时有效,但我想只显示最后一个“printf”并删除所有其他“printf”。 顺便说一句,我在窗口上,而不是在 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;
}
答:
2赞
ikegami
11/1/2023
#1
您同时包含特定于 unix 的头文件 (unistd.h) 和特定于 Windows 的头文件 (windows.h),因此我猜您是在 Unix 仿真环境中运行,例如 Cygwin、MSYS 和 MSYS2 提供的环境。
作为 unix 仿真,它们使用 而不是 to 执行传递给 的 shell 命令。由于是内置的,这是行不通的。sh
cmd
system
cls
cmd
执行可能会起作用。
执行会起作用。clear
cmd /c cls
最后,由于您在 unix 仿真中运行,因此以下操作也可能有效:
printf( "\x1B[H\x1B[2J\x1B[3J" );
fflush( stdout );
评论
1赞
chqrlie
11/2/2023
您还可以建议一种更简单的方法:删除呼叫。printf(" tempsrestant : %3d sec\r", tempsrestant); fflush(stdout);
system("cls")
上一个:如何在运行时挂钩函数
评论
system()
cls
#include <unistd.h>
#include <windows.h>
sh
cmd
system
cls
cmd
printf(...),
sleep()
fflush(stdout);
fflush
system
system("cls")