0

I want to convert the strFileNotifyInfo[1].FileName(Wchar_t) to a string so that i can see the filepath. 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;
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
user1812707
  • 67
  • 1
  • 7

2 Answers2

0

Use wcout when working with wide character data.

std::wcout << L"File Modified: " << strFileNotifyInfo[1].FileName << std::endl;
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
0

You should also keep in mind that 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;
y0prst
  • 611
  • 1
  • 6
  • 13