Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在msgbox中显示当前系统时间_C++_Mfc_Visual C++ 6 - Fatal编程技术网

C++ 在msgbox中显示当前系统时间

C++ 在msgbox中显示当前系统时间,c++,mfc,visual-c++-6,C++,Mfc,Visual C++ 6,当我编译下面的代码时,我遇到了一个错误,这个错误告诉我必须将unsigned short转换为char,我不知道如何执行这个转换。我用的是VC6MFC。这是我使用的代码 SYSTEMTIME st; GetSystemTime(&st); unsigned short time = st.wHour; MessageBoxA(TEXT(time),"system time",MB_OK); 您可以使用std::ostringstream: #include <sstream>

当我编译下面的代码时,我遇到了一个错误,这个错误告诉我必须将unsigned short转换为char,我不知道如何执行这个转换。我用的是VC6MFC。这是我使用的代码

SYSTEMTIME st;
GetSystemTime(&st);
unsigned short time = st.wHour;
MessageBoxA(TEXT(time),"system time",MB_OK);

您可以使用
std::ostringstream

#include <sstream>

...

std::ostringstream time;
time << st.wHour;
MessageBoxA(time.str().c_str(),"system time",MB_OK);
#包括
...
std::ostringstream时间;

时间变量
time
是一个整数,而
MessageBoxA
需要一个字符串。您必须将整数转换为字符串,这不是由
TEXT
宏完成的。我建议您阅读更多关于
std::stringstream
或可能的
sprintf

char-str[20]

sprintf(str,“%d”,st.wHour)

MessageBoxA(str,“系统时间”,MB_OK)

罗德泰勒


沙特阿拉伯

鉴于这是一个MFC项目,我认为CString::Format()比sprintf()更可取。