2013-02-10 75 views
0

我想將strFileNotifyInfo [1] .FileName(Wchar_t)轉換爲字符串,以便我可以看到文件路徑。但我不能讓它工作。Wchar_t to string轉換

這裏是我的代碼:

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

我懷疑這是'wchar_t'。你的意思是'wchar_t *'? – 2013-02-10 16:46:03

+0

嘗試WideCharToMultiByte API http://msdn.microsoft.com/en-us/library/windows/desktop/dd374130%28v=vs.85%29.aspx – Nika 2013-02-10 16:51:49

+0

對不起,我的壞,它是一個Wchar_t *。 – user1812707 2013-02-10 17:09:12

回答

0

使用wcout與寬字符數據時。

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

你還應該記住,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;