2017-05-25 5 views
-2
#include <stdio.h> 
#include <time.h> 
int main() 
{ 
    time_t now; 
    struct tm timespace; 
    double seconds; 
    struct timespec t = { 2 /*seconds*/, 0 /*nanoseconds*/ }; 
    while (1) { 
     time(&now); 
     seconds = now; 
     printf("%.f Timer Counter\n", seconds); 
     timespace = *localtime(&now); 
     nanosleep(&t, NULL); 
     time(&now); 
     seconds = now; 
     fflush(stdout); 
     printf("%.f Counter Value After two second delay.\n Hellow.\n", seconds); 
     seconds = now; 
     printf("\n\n\n"); 
     timespace = *localtime(&now); 
     nanosleep(&t, NULL); 
     fflush(stdout); 

     time(&now); 
     seconds = now; 
     printf("%.f Counter Value After two second delay.\n Hello World 2.\n", seconds); 
     return 0; 
    } 
} 

私はそれが言う、このコードをコンパイルするたびに使用して2秒の遅延後に表示テキストをすべきコードを書くしようとすると:出力はインターバルタイマ

variable `timespec t' has initializer but incomplete type 

'nanosleep' undeclared (first use this function) 
+0

ナノスリープはLinuxのみです。 Windowsの場合はスリープを使用する必要があります。クロスプラットフォームのC++が必要な場合は、std :: chrono –

答えて

0

私はこのような何かをする時間の遅延をしたい場合。

int t = time(NULL); 
int delay = 2; 

    while(time(NULL) < (t + delay)); 
    std::cout << "..."; 

かなり近い2秒の時間です。