简体   繁体   English

Wchar_t到字符串的转换

[英]Wchar_t to string Conversion

I want to convert the strFileNotifyInfo[1].FileName (Wchar_t) to a string so that i can see the filepath. 我想将strFileNotifyInfo [1] .FileNameWchar_t )转换为字符串,以便可以看到文件路径。 but i can't make it work. 但我不能使它工作。

Here is my code: 这是我的代码:

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;
    }
}

处理宽字符数据时,请使用wcout

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

You should also keep in mind that FileName is not null-terminated . 您还应该记住FileName不能为null终止

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM