【发布时间】:2019-09-07 00:03:10
【问题描述】:
我试图让我的代码在屏幕上打印一些东西,然后等待 1 秒钟,然后绕过 for 循环并再次打印 21 次。当我在 Windows 中的 CodeBlocks 中使用#include 然后使用 Sleep(1000) 时,它可以工作。但是当我使用#include 和 sleep(1) 在我的 Ubuntu VM 上执行此操作时,所有内容都会从我的终端消失 21 秒,然后立即全部出现。我认为我使用了错误的功能或其他东西。
有什么想法吗?
这是 Ubuntu 终端中的代码,它最终会删除我终端上已经存在的所有内容,等待 21 秒,然后只打印“Hello”21 次。
#include <stdio.h>
#include <unistd.h>
int main()
{
for (int i = 0; i < 21; i++)
{
printf("Hello");
sleep(1);
}
}
这是 Windows 中的代码,它每秒打印“Hello”,持续 21 秒,因此在 21 秒内在我的屏幕上打印 21 个 Hello。这就是我想要在我的 Ubuntu VM 中实现的目标。
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main() {
for (int i = 0; i < 21; i++)
{
printf("Hello");
Sleep(1000);
}
return 0;
}
【问题讨论】:
-
将
printf替换为puts。 -
投票结束:OP 承认他们提供的 MCVE 与他们正在解决的实际问题不同,提出新问题。引用 OP:
I tried adding the fflush(stdout); like you suggested, but it doesn't work for my code - probably because I'm using mvprintw (from ncurses) instead of printf