读书人

获取系统时间出错 兄弟们帮下忙解决办

发布时间: 2012-04-17 15:06:33 作者: rapoo

获取系统时间出错 兄弟们帮下忙
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <time.h>
#include <stdio.h>
#include <string>
#include <atlstr.h>
using namespace std;
int main()
{ SYSTEMTIME lpSystemTime;
GetLocalTime(&lpSystemTime);
CString tim;
tim.Format( "%d 年 %d 月 %d日 星期 %d %d:%d:%d:%d ",lpSystemTime.wYear,lpSystemTime.wMonth,lpSystemTime.wDay,lpSystemTime.wDayOfWeek,lpSystemTime.wHour,lpSystemTime.wMinute,lpSystemTime.wSecond,lpSystemTime.wMilliseconds);
cout<<tim<<endl;
system("pause");
}
--------------------------------------------
编译时这样
1> Timetest.cpp
1>e:\study\vc++\timetest\timetest\timetest.cpp(33): error C2664: “void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)”: 不能将参数 1 从“const char [52]”转换为“const wchar_t *”
1> with
1> [
1> BaseType=wchar_t,
1> StringTraits=ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t>>
1> ]
1> 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
1>
1>生成失败。
-----------------------------------
要怎么改呢

[解决办法]
你的工程是 UNICODE的 但 tim.Format 的参数你用的是 ANSI 的
要使用 使用 _T 宏或者 TEXT宏之类的。..

当然,修改为非UNOCODE的也行
[解决办法]
format("",XXXXXX)改成
format(_T(""), XXXX)就OK
[解决办法]
tim.Format( "%d 年 %d 月 %d日 星期 %d %d:%d:%d:%d ",lpSystemTime
=============
UNICODE的问题,改成这样:
tim.Format( _T("%d 年 %d 月 %d日 星期 %d %d:%d:%d:%d "),lpSystemTime.......);也就是凡是字符串常量都用_T(...)括起来
[解决办法]
把_T改成TEXT会更好些,有的时候会找不到_T的宏
[解决办法]
cout<<tim<<endl;
-----------
你这样中文是显示不了,可以使用WriteConsole();

读书人网 >VC/MFC

热点推荐