5

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.

Fiju
  • 416
  • 1
  • 5
  • 11

4 Answers4

4

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

roalz
  • 2,699
  • 3
  • 25
  • 42
frp
  • 1,119
  • 1
  • 10
  • 30
1

In c++11, you can create a thread that loop on sleep 3sec and print dots just before reading the file. You must use condition or variable guarded by mutex to exit the thread.

Renaud
  • 209
  • 2
  • 9
0

2 basic solutions:

1 use sleep() of usleep() to block it. 2. use a while loop to let cpu busy loop.

In production, we usualy do the first solution. But be cautious about this always. make sure this is what you want to do.

Another way is, use a event loop approach and set a timer for this event. it can be ran later after timer is timed-out

BufBills
  • 8,005
  • 12
  • 48
  • 90
0

In C++11 you can use:

#include <chrono>
#include <thread>
...
using namespace std::chrono_literals;
...
std::this_thread::sleep_for(2s);
Casimir Kash
  • 42
  • 1
  • 1
  • 10