Corrupted vector entries with LPCWSTR vector

2020-05-06 17:22发布

I ahve the following piece of code. I get a correctly filled vector. But I am unable to print or use the vector contents which are file names from a directory. As soon as I do enter the first iteration. Everything gets lost. What am I doing wrong?

wprintf - This works OK

wcout-- here is where everything ends up corrupted

#include <windows.h>
#include <sstream>
#include <string>
#include <vector>
#include<iostream>
void GetAllFiles(vector<LPCWSTR>&, wstring);
using namespace std;
void main (void)
{
    vector<LPCWSTR> files(0);
    wstring path = L"Datasets\\Persons\\";
    wstring ext = L"*.*";
    wstring fullPath = path+ext;
    GetAllFiles(files,fullPath);    
    for (unsigned i=0; i<files.size() ; i++)
    {
        try
        {
            wcout<<"::\n"<<files[i];
        }
        catch(exception &ex)
        {
            cout<<"Error:"<<ex.what();
        }
    }

}

void GetAllFiles(vector<LPCWSTR>& fileNames,wstring dir)
{

    WIN32_FIND_DATA search_data;
    memset(&search_data, 0, sizeof(WIN32_FIND_DATA));
    HANDLE handle = FindFirstFile(dir.c_str(),&search_data);
    while(handle != INVALID_HANDLE_VALUE)
    {
        wprintf(L"Found file: %s\r\n", search_data.cFileName);
        fileNames.push_back(search_data.cFileName);
        if(FindNextFile(handle, &search_data) == FALSE)
            break;
    }   
}

I have attached a screen shots of the output.

Correct when read off disk in GetAllFiles(...)

Corrupted as soon as the loops first iteration is done

2条回答
Rolldiameter
2楼-- · 2020-05-06 17:45

Probably that's happening because you pass local variable to push_back(). I'm not sure here, but what could happen here: push_back expects object of type LPCWSTR, while you passing char* instead. I don't know, how this conversion is done, but probably the pointer is just copied, and the value of this pointer becomes invalid whenyou return from the function - try explicit copying the strings before passing them to push_back.

查看更多
ゆ 、 Hurt°
3楼-- · 2020-05-06 18:08

search_data.cFileName is a pointer to memory controlled by the FindFirstFile/FindNextFile iterator interface; you cannot store this pointer value as the pointed-to memory could change from iteration to iteration (or even be freed after the iteration completes).

Instead, you must make a copy of the string to put in your vector, e.g. using wcsdup. Even better, define your vector as a vector<wstring>, so that push_back(search_data.cFileName); creates a wstring with the contents of search_data.cFileName.

查看更多
登录 后发表回答