【发布时间】:2022-01-10 17:16:56
【问题描述】:
嘿,这可能是一个愚蠢的初学者问题。
我想从 const char* array[] 中的文件夹中写入所有 .txt 文件的文件名。
所以我试着这样做:
const char* locations[] = {"1"};
bool x = true;
int i = 0;
LPCSTR file = "C:/Folder/*.txt";
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = FindFirstFile(file, &FindFileData);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
locations.append(FindFileData.cFileName); //Gives an error
i++;
}
while (FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
cout << "number of files " << i << endl;
基本上,代码应该将 .txt 文件的文件名添加到 const char* 位置数组,但使用 append 不起作用,因为它给了我错误:“C++ 表达式必须具有类类型,但它具有类型'' const char *''"
那我该怎么做呢?
【问题讨论】:
-
简答:使用
std::vector<std::string>。 -
嘿@Brian 你想让我改用 std::vector<:string> 吗?我需要 const char* locations[] 格式的数组列表框。我将如何转换?尝试使用 std::vector<:string>locations;和位置.push_back(FindFileData.cFileName);现在。我怎样才能把它变成一个数组?
-
使用向量。然后
locations[ i ].data()会将char*指针返回到std::string的底层缓冲区。