I’m having a problem with vectors which I tried to solve for quite some time now. I will just post the code right away and explain it then.
#include "filesystem.h"
#include <vector>
using namespace Filesystem;
wchar_t** Path::ListDir()
{
/*struct dirEntry
{
wchar_t entry[MAX_PATH];
};*/
vector<wchar_t*> pathList = vector<wchar_t*>();
WIN32_FIND_DATA findData;
HANDLE dirHandle = new HANDLE;
wchar_t* path = L"C:\\*";
dirHandle = FindFirstFile(path, &findData);
if (dirHandle == INVALID_HANDLE_VALUE)
return NULL;
while (FindNextFile(dirHandle, &findData) != 0)
{
pathList.push_back(findData.cFileName);
}
FindClose(dirHandle);
return NULL;
}
This is part of a filesystem class I am coding right now. It’s supposed to list all the files in a directory and return them as a 2 dimensional array of strings. If I do it like this, it fills up the list with the last file times the count of files in the folder. Which is not what I want it to do. I thought that it could be since I deliver a pointer to the push_back, but I haven’t found a way to fix it. I’m coding on windows, btw.
It would be nice if someone on here has any idea on how to do that.
You are storing the address of
findData.cFileNamein thevectorN times.You need to copy this. Possibly using:
EDIT: Other minor points:
can be replaced with:
If you must return a
wchar_t**then (for example):The caller must remember to
delete[]returned array:Though as this is C++ you should use
std::vector<std::wstring>.