如何在C ++中显示实时 [英] How to display real time in c++

查看:58
本文介绍了如何在C ++中显示实时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我如何使用c ++实时显示.我的意思是,程序运行时,您可以看到秒数或分钟数倒计时,就像挂在墙上的真实时钟一样.

Can some one tell me how do i display real time in c++. what i mean is that while the program is running you can see the seconds and or minutes counting down like a real clock hanging on the wall

这就是我所拥有的:

int main ()
{
  time_t rawtime; //creates and object of the built in time function
  struct tm * timeinfo; //no idea what this do

  time ( &rawtime ); //gets the time from the computer
  timeinfo = localtime ( &rawtime ); //store that time here

  //it displays current date and time except time is frozen and not real time
  cout<< "Current local time and date: "<<asctime (timeinfo)<< endl; 


  system("pause");
  return 0;

}

推荐答案

不是在C ++中(在C/Win32中),但是可以.

Not in C++ (in C/Win32) but works.

#include <stdio.h>
#include <windows.h>


int _tmain(int argc, _TCHAR* argv[])
{
    SYSTEMTIME stime;   //structure to store system time (in usual time format)
    FILETIME ltime;     //structure to store local time (local time in 64 bits)
    FILETIME ftTimeStamp;
    char TimeStamp[256];//to store TimeStamp information
    while (true){
    ////Prepare data needed to output the time stamp:
    GetSystemTimeAsFileTime(&ftTimeStamp); // Gets the current system time
    FileTimeToLocalFileTime (&ftTimeStamp,&ltime);//convert in local time and store in ltime
    FileTimeToSystemTime(&ltime,&stime);//convert in system time and store in stime

    sprintf(TimeStamp, "%d:%d:%d, %d.%d.%d \r",stime.wHour,stime.wMinute,stime.wSecond, stime.wDay,stime.wMonth,stime.wYear);
    printf(TimeStamp);

    Sleep(1000);
    }

    system("pause");
    return 0;
} 

这篇关于如何在C ++中显示实时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆