如何每秒打印一个单词?

时间:2016-04-27 02:46:46

标签: c++

如何在使用fopen("filename.txt","r")时每秒打印一次单词?

我之前搜索过并找到了使用unistd.h头文件的内容,但在我的Turbo C ++中没有这样的头文件。

1 个答案:

答案 0 :(得分:4)

C ++ 11

如果您的编译器支持C ++ 11,std::thread::sleep_for是最佳的跨平台解决方案:

#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);

如果您被迫使用不支持C ++的编译器,则必须使用特定于平台的函数:

POSIX

sleep是POSIX.1-2001的一部分,因此它应该在任何兼容的操作系统上运行,包括UNIX,Linux和Mac OS X:

#include <unistd.h>

sleep(1);

Sleep是WinAPI的一部分:

#include <windows.h>

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