【发布时间】:2016-08-23 13:53:26
【问题描述】:
我得到了以下代码:
int _tmain(int argc, _TCHAR* argv[]) {
_finddata_t dirEntry;
intptr_t dirHandle;
dirHandle = _findfirst("C:/*", &dirEntry);
int res = (int)dirHandle;
while(res != -1) {
cout << dirEntry.name << endl;
res = _findnext(dirHandle, &dirEntry);
}
_findclose(dirHandle);
cin.get();
return (0);
}
它的作用是打印给定目录 (C:) 包含的所有内容的名称。现在我必须打印出子目录中所有内容的名称(如果有的话)。到目前为止,我已经得到了这个:
int _tmain(int argc, _TCHAR* argv[]) {
_finddata_t dirEntry;
intptr_t dirHandle;
dirHandle = _findfirst(argv[1], &dirEntry);
vector<string> dirArray;
int res = (int)dirHandle;
unsigned int attribT;
while (res != -1) {
cout << dirEntry.name << endl;
res = _findnext(dirHandle, &dirEntry);
attribT = (dirEntry.attrib >> 4) & 1; //put the fifth bit into a temporary variable
//the fifth bit of attrib says if the current object that the _finddata instance contains is a folder.
if (attribT) { //if it is indeed a folder, continue (has been tested and confirmed already)
dirArray.push_back(dirEntry.name);
cout << "Pass" << endl;
//res = _findfirst(dirEntry.name, &dirEntry); //needs to get a variable which is the dirEntry.name combined with the directory specified in argv[1].
}
}
_findclose(dirHandle);
std::cin.get();
return (0);
}
现在我不要求提供整个解决方案(我希望能够自己完成),但只有一件事我无法理解,那就是 TCHAR* argv。我知道 argv[1] 包含我在“命令参数”下的项目属性中放置的内容,现在它包含我想要在 (C:/users/name/New folder/*) 中测试我的应用程序的目录,其中包含一些带有子文件夹的文件夹和一些随机文件。 argv[1] 当前给出以下错误:
错误:“_TCHAR*”类型的参数与“const char *”类型的参数不兼容
现在我搜索了 TCHAR,我知道它是 wchar_t* 或 char*,具体取决于使用 Unicode 字符集或多字节字符集(我目前使用的是 Unicode)。我也明白转换是一个巨大的痛苦。所以我要问的是:我怎样才能最好地使用 _TCHAR 和 _findfirst 参数解决这个问题?
我打算将 dirEntry.name 连接到 argv[1] 并在末尾连接一个“*”,并在另一个 _findfirst 中使用它。由于我仍在学习 C++,因此我的代码中的任何 cmets 都会受到赞赏。
【问题讨论】:
-
_TCHAR是一个应该在“windows.h”中定义的宏。您是否包含此标头? -
我正在使用带有预编译头的 Visual Studio 2015,所以我猜是这样