Skip to content
Advertisement

delay output in c++ on linux

I want some random characters to be printed to console and then deleted by "b". After this I want to put my own variable so it will be looking like “randomizing”. The problem is that it is happening too fast. I wanted to delay the output by using usleep or sleep function but when I’m using it, nothing is printed into console.
Short example:

#include <iostream>
#include <unistd.h>
using namespace std;

int main()
{
    char chars[]={'a','b','c','g','h','u','p','e','y'};
    for(int i=0; i<8; i++)
    {
        cout << chars[i];
        usleep(200000);
        cout << "b";

    }
}

Advertisement

Answer

Try my little program slowtty from github.

It allows you to simulate in a pty the behaviour of an old rs232c line, by delaying the output per character as stty(1) command allows to set the baudrate.

You call it with

$ slowtty
$ stty 1200
$

and the terminal begins to write characters at a slow pace (like a 1200baud line)

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement