简体   繁体   English

如何每秒打印一个单词?

[英]How to print a word every second?

How can I print a word once a second while using fopen("filename.txt","r") ? 如何在使用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. 我搜索较早,发现使用了unistd.h头文件,但是在我的Turbo C ++中不存在这样的头文件。

C++11 C ++ 11

If your compiler supports C++11, std::thread::sleep_for is the best, cross-platform solution: 如果您的编译器支持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);

If you are forced to use compiler that doesn't support C++, you have to use platform-specific functions: 如果您被迫使用不支持C ++的编译器,则必须使用特定于平台的功能:

POSIX POSIX

sleep is part of POSIX.1-2001, so it should run on any compliant operating system, including UNIXes, Linux and Mac OS X: sleep是POSIX.1-2001的一部分,因此它应在任何兼容的操作系统上运行,包括UNIX,Linux和Mac OS X:

#include <unistd.h>

sleep(1);

Windows 视窗

Sleep is part of WinAPI: Sleep是WinAPI的一部分:

#include <windows.h>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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