【问题标题】:Wchar_t to string Conversionwchar_t 到字符串的转换
【发布时间】:2013-01-25 20:01:16
【问题描述】:

我想将 strFileNotifyInfo[1].FileName(Wchar_t) 转换为字符串,以便我可以看到文件路径。但我不能让它工作。

这是我的代码:

while(TRUE)
{
    if( ReadDirectoryChangesW( hDir, (LPVOID)&strFileNotifyInfo, sizeof(strFileNotifyInfo), FALSE, FILE_NOTIFY_CHANGE_LAST_WRITE || FILE_NOTIFY_CHANGE_CREATION, &dwBytesReturned, NULL, NULL) == 0)
    {
        cout << "Reading Directory Change" << endl;
    }
    else
    {

        cout << ("File Modified: ") << strFileNotifyInfo[1].FileName << endl;
        cout << ("Loop: ") << nCounter++ << endl;
    }
}

【问题讨论】:

标签: c++ string wchar readdirectorychangesw


【解决方案1】:

您还应该记住FileName is not null-terminated

WCHAR* filename_w = strFileNotifyInfo[1].FileName;
DWORD filename_len = strFileNotifyInfo[1].FileNameLength;
std::string filename(filename_w, filename_w + filename_len);
std::cout << "File Modified: " << filename << std::endl;

【讨论】:

    【解决方案2】:

    在处理宽字符数据时使用wcout

    std::wcout << L"File Modified: " << strFileNotifyInfo[1].FileName << std::endl;
    

    【讨论】:

      猜你喜欢
      • 2015-02-15
      • 1970-01-01
      • 2014-04-27
      • 2018-09-02
      • 2014-11-12
      • 1970-01-01
      • 2011-02-03
      • 2011-04-16
      • 2018-08-12
      相关资源
      最近更新 更多