简体   繁体   中英

How to print a word every second?

How can I print a word once a second while using fopen("filename.txt","r") ?

I searched earlier and found something that used the unistd.h header file, but in my Turbo C++ no such header file exists.

C++11

If your compiler supports C++11, std::thread::sleep_for is the best, cross-platform solution:

#include <chrono>
#include <thread>

std::this_thread::sleep_for(std::chrono::seconds(1));
// or
using namespace std::chrono_literals;
std::this_thread::sleep_for(1s);

If you are forced to use compiler that doesn't support C++, you have to use platform-specific functions:

POSIX

sleep is part of POSIX.1-2001, so it should run on any compliant operating system, including UNIXes, Linux and Mac OS X:

#include <unistd.h>

sleep(1);

Windows

Sleep is part of WinAPI:

#include <windows.h>

Sleep(1000); // Note capital "S" and that you specify time in milliseconds

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM