[英]C++ _findfirst and TCHAR
I've been given the following code: 我得到了以下代码:
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);
}
what this does is printing the name of everything that the given directory (C:) contains. 这样做是打印给定目录(C :)包含的所有内容的名称。 Now I have to make this print out the name of everything in the subdirectories (if there are any) as well.
现在我必须打印出子目录中所有内容的名称(如果有的话)。 I've got this so far:
到目前为止我有这个:
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);
} }
Now I'm not asking for the whole solution (I want to be able to do it on my own) but there is just this one thing I can't get my head around which is the TCHAR* argv. 现在我不是要求整个解决方案(我希望能够自己完成)但是有一件事我无法理解这是TCHAR * argv。 I know argv[1] contains what I put in my project properties under "command arguments", and right now this contains the directory I want to test my application in (C:/users/name/New folder/*), which contains some folders with subfolders and some random files.
我知道argv [1]包含我在“命令参数”下的项目属性中放置的内容,现在它包含我想要在(C:/ users / name / New folder / *)中测试我的应用程序的目录,其中包含一些包含子文件夹和一些随机文件的文件夹。 The argv[1] currently gives the following error:
argv [1]目前给出以下错误:
Error: argument of type "_TCHAR*" is incompatible with parameter of type "const char *"
错误:类型“_TCHAR *”的参数与“const char *”类型的参数不兼容
Now I've googled for the TCHAR and I understand it is either a wchar_t* or a char* depending on using Unicode character set or multi-byte character set (I'm currently using Unicode). 现在我用Google搜索了TCHAR,我知道它是wchar_t *或char *取决于使用Unicode字符集或多字节字符集(我目前正在使用Unicode)。 I also understand converting is a massive pain.
我也明白转换是一个巨大的痛苦。 So what I'm asking is: how can I best go around this with the _TCHAR and _findfirst parameter?
所以我要问的是:我怎样才能最好地利用_TCHAR和_findfirst参数解决这个问题?
I'm planning to concat the dirEntry.name to the argv[1] as well as concatenating a "*" on the end, and using this in another _findfirst. 我打算将dirEntry.name连接到argv [1],并在最后连接一个“*”,并在另一个_findfirst中使用它。 Any comments on my code are appreciated as well since I'm still learning C++.
我对代码的任何评论都很受欢迎,因为我还在学习C ++。
See here: _findfirst is for multibyte strings while _wfindfirst
is for wide characters. 请参见此处: _findfirst用于多字节字符串,而
_wfindfirst
用于宽字符。 If you use TCHAR in your code then use _tfindfirst
(macro) which will resolve to _findfirst on non UNICODE, and _wfindfirst on UNICODE builds. 如果在代码中使用TCHAR,则使用
_tfindfirst
(宏)将解析为非UNICODE上的_findfirst,以及UNICODE构建时的_wfindfirst。
Also instead of _finddata_t use _tfinddata_t which will also resolve to correct structure depending on UNICODE config. 而不是_finddata_t使用_tfinddata_t,它也将根据UNICODE配置解析为正确的结构。
Another thing is that you should use also correct literal, _T("C:/*")
will be L"C:/*"
on UNICODE build, and "C:/*"
otherwise. 另一件事是你应该使用正确的文字,
_T("C:/*")
在UNICODE构建时将是L"C:/*"
,否则将是"C:/*"
。 If you know you are building with UNICODE defined, then use std::vector<std::wstring>
. 如果您知道使用UNICODE定义构建,则使用
std::vector<std::wstring>
。
btw. 顺便说一句。 Visual Studio by default will create project with UNICODE, you may use only wide versions of functions like
_wfindfirst
as there is no good reason to build non UNICODE projects. 默认情况下,Visual Studio将使用UNICODE创建项目,您可能只使用
_wfindfirst
等广泛版本的函数,因为没有充分的理由来构建非UNICODE项目。
TCHAR and I understand it is either a wchar_t* or a char* depending on using UTF-8 character set or multi-byte character set (I'm currently using UTF-8).
TCHAR和我理解它是wchar_t *或char *取决于使用UTF-8字符集或多字节字符集(我目前使用的是UTF-8)。
this is wrong, in UNICODE windows apis uses UTF-16. 这是错误的,在UNICODE窗口中,apis使用UTF-16。
sizeof(wchar_t)==2
. sizeof(wchar_t)==2
。
Use this simple typedef
: 使用这个简单的
typedef
:
typedef std::basic_string<TCHAR> TCharString;
Then use TCharString
wherever you were using std::string
, such as here: 然后在使用
std::string
地方使用TCharString
,例如:
vector<TCharString> dirArray;
See here for information on std::basic_string . 有关std :: basic_string的信息,请参见此处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.