Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 基于wchar_t*的向量的奇怪行为_C++_Winapi_Vector_Stl - Fatal编程技术网

C++ 基于wchar_t*的向量的奇怪行为

C++ 基于wchar_t*的向量的奇怪行为,c++,winapi,vector,stl,C++,Winapi,Vector,Stl,我正在开发一个小函数,它遍历一个目录,并将所有文件的名称放入基于wchar\u t*的向量中: #include <Windows.h> #include <tchar.h> #include <vector> #include <Shlwapi.h> #pragma comment(lib, "Shlwapi.lib") int _tmain(int argc, _TCHAR* argv[]) { WIN32_FIND_DATA ff

我正在开发一个小函数,它遍历一个目录,并将所有文件的名称放入基于
wchar\u t*
的向量中:

#include <Windows.h>
#include <tchar.h>
#include <vector>

#include <Shlwapi.h>
#pragma comment(lib, "Shlwapi.lib")

int _tmain(int argc, _TCHAR* argv[])
{
    WIN32_FIND_DATA ffd;

    std::wstring sDir(L"D:\\Test");
    sDir += L"\\*"; 

    HANDLE hFind = INVALID_HANDLE_VALUE;
    hFind = FindFirstFile(sDir.c_str(), &ffd);

    std::vector<wchar_t*> vctsFiles;
    wchar_t szBuf[MAX_PATH];

    do
    {
        if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
        {
            memset(szBuf, 0, sizeof(szBuf));
            ::PathCombine(szBuf, L"D:\\Test", ffd.cFileName);
            vctsFiles.push_back(szBuf);
        }

    } while(FindNextFile(hFind, &ffd) != 0);

    return 0;
}
#包括
#包括
#包括
#包括
#pragma注释(lib,“Shlwapi.lib”)
int _tmain(int argc,_TCHAR*argv[]
{
WIN32_查找_数据ffd;
std::wstring sDir(L“D:\\Test”);
sDir+=L“\\*”;
句柄hFind=无效的句柄值;
hFind=FindFirstFile(sDir.c_str(),&ffd);
std::矢量vcts文件;
wchar_t szBuf[MAX_PATH];
做
{
if(!(ffd.dwFileAttributes和文件属性目录))
{
memset(szBuf,0,sizeof(szBuf));
::路径组合(szBuf,L“D:\\Test”,ffd.cFileName);
vctsFiles.push_back(szBuf);
}
}while(FindNextFile(hFind,&ffd)!=0);
返回0;
}
问题是向量中的所有条目在附加新文件名后都会被覆盖。换句话说,所有向量项将包含相同的值


我不太清楚为什么会发生这种情况,因为我总是在将缓冲区用于新条目之前清除它。有没有办法补救这种行为?任何建议都将不胜感激。

您的向量是指针向量,它们都指向同一个位置(
szBuf
)。每次都需要分配一个
新的szBuf
,或者更好地使用wstring向量

std::vector<std::wstring> vctsFiles;
std::vector vcts文件;

您的向量是指针向量,它们都指向同一个位置(
szBuf
)。每次都需要分配一个
新的szBuf
,或者更好地使用wstring向量

std::vector<std::wstring> vctsFiles;
std::vector vcts文件;

重复使用同一个缓冲区,但一次又一次地将其地址放在向量上。相反,您需要新字符串。为什么不使用一个
std::vector
?哦,一旦你了解了它在做什么,这就不是什么奇怪的行为了(换句话说,它工作得非常好,只是不是你所期望的那样)。你重复使用同一个缓冲区,但一次又一次地将它的地址放在向量上。相反,您需要新字符串。为什么不使用一个
std::vector
?哦,一旦你理解了它在做什么(换句话说,它工作得非常好,只是不是你所期望的)。或者std::array,因为缓冲区大小是fixed@paulm嗯,没有。目录的数量是未知的,所以你不会用
std::array
@crashmstr:我相信paulm的意思是
std::array
代替
std::wstring
,而不是代替向量。(我并不同意)正如@Benjamin Lindley所说:)你可以很容易地将一个C接口包装在其中的任何一个上,指向std::array或std::string::C_str的元素0。@Aurora如果你愿意限制目录的最大数量,你只能使用固定数组,例如完全抛弃向量并使用
wchar_t szBuf[500][1024]
。或std::array,因为缓冲区大小为fixed@paulm嗯,不。目录的数量是未知的,所以你不会使用
std::array
@crashmstr:我相信paulm的意思是
std::array
代替
std::wstring
,而不是代替向量。(我不同意)正是@Benjamin Lindley所说的:)您可以很容易地在这两个元素的周围包装一个C接口,指向std::array或std::string::C_str的元素0。@Aurora如果您愿意限制目录的最大数量,您只能使用固定数组,例如完全抛弃向量并使用
wchar_t szBuf[500][1024]