How to delay output in C++?

c++, delay, output

Solution

This is platform-dependent.

On Windows, you may use Sleep() function:

Sleep(3000);

The parameter is the amount of time in milliseconds. You need to include windows.h in order to use this function.

On POSIX-compatible, you may use sleep():

sleep(3); // parameter is in seconds

If you need better precision, you may use usleep():

usleep(3000000); // parameter is in microseconds

For both functions, you need to include unistd.h

Problem

I am doing a project that need to read data from a file. But before the file is opened i want to display `Loading . . . . .` I want the dots after "Loading" to get printed one by one and each after about 2-3 seconds and then i display my file's contents. That is, after first 3 seconds `Loading .` displays and after another 3 seconds `Loading . .` displays and so on. I Couldn't find a way to do, please help.

Original source