TCHAR szFileTime[100];
SYSTEMTIME systime;
memset(&systime, 0, sizeof(SYSTEMTIME));
GetSystemTime(&systime);
_stprintf(szFileTime, _T("%d-%d-%d %.2d:%.2d:%.2d"), systime.wYear, systime.wMonth, systime.wDay, systime.wHour, systime.wMinute, systime.wSecond);string s = string(szFileTime);//这里怎么转?outFile<<setw(100)<<string;程序中想以string方式输出文件,现在将数据用 tchar[]数组存储好,请问如何把tchar数组转化为string,用来输出?

解决方案 »

  1.   

    int U2A(const wchar_t* szU,char* szA,size_t cnt)
    {
    return WideCharToMultiByte (CP_ACP, 0, szU, -1, szA, cnt, NULL, NULL) ;
    }
    int A2U(const char* szA,wchar_t* szU,size_t cnt)
    {
    return MultiByteToWideChar (CP_ACP, 0, szA, -1, szU, cnt) ;
    }
    std::string  U2A(const wchar_t* szU)
    {
    int nRetCode=U2A(szU,0,0);
    if(0==nRetCode)
    return std::string();
    std::string str(nRetCode-1,'\0');
    U2A(szU,(char*)(str.c_str()),nRetCode);
    return str;
    }
    std::wstring A2U(const char* szA)
    {
    int nRetCode=A2U(szA,0,0);
    if(0==nRetCode)
    return std::wstring();
    std::wstring str(nRetCode-1,'\0');
    A2U(szA,(wchar_t*)(str.c_str()),nRetCode);
    return str;
    }
    如下调用:
    string s;
    if(sizeof(TCHAR)==sizeof(char))
    s = szFileTime;
    else
    s = U2A(szFileTime);
      

  2.   

    WinCE是Unicode的,TCHAR就是相当于wchar_t,你需要将TCHAR转成char,WideCharToMultiByte();