为什么在usleep()延迟之间不能打印std :: cout和printf()? [英] Why do std::cout and printf() not print between usleep() delays?

查看:147
本文介绍了为什么在usleep()延迟之间不能打印std :: cout和printf()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应该打印"Hello,World!"的程序.在缓慢滚动的文本中.我正在将unistd.h库用于usleep()函数,并且我正在使用std :: cout将字符打印到标准输出中:

i have a program that is supposed to print "Hello, World!" in slowly scrolling text. I am using the unistd.h library for the usleep() function, and i'm using std::cout to print the characters to the standard output:

    #include <iostream> 
    #include <stdio.h>   
    #include <unistd.h>
    char hello[13]={'H','e','l','l','o',',',' ','W','o','r','l','d'};

    int main (){
         for(int i=0; i<14; i++){
              std::cout<<hello[i]; //it prints the entire string after
              usleep(100000);      //100000 ms, but it should print a char after
              }                    //every 100 ms.
    }

推荐答案

您可能需要刷新输出流.

You might need to flush the output stream.

这篇关于为什么在usleep()延迟之间不能打印std :: cout和printf()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆