Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.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++ C++_findfirst和TCHAR_C++_Argv_Tchar_Findfirst - Fatal编程技术网

C++ C++_findfirst和TCHAR

C++ C++_findfirst和TCHAR,c++,argv,tchar,findfirst,C++,Argv,Tchar,Findfirst,我得到了以下代码: 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 << en

我得到了以下代码:

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);
}
int-tmain(int-argc,_-TCHAR*argv[]{
_finddata_t dirEntry;
intptr_t dirHandle;
dirHandle=_findfirst(“C://*”,&dirEntry);
int res=(int)dirHandle;
而(res!=-1){

不能使用这个简单的
typedef

typedef std::basic_string TCharString;

然后在使用
std::string
的地方使用
TCharString
,例如:

vector<TCharString> dirArray;
矢量dir阵列;
有关的信息,请参见此处。

参见此处:用于多字节字符串,而
\u wfindfirst
用于宽字符。如果在代码中使用TCHAR,则使用
\u tfindfirst
(宏),该宏将解析为非UNICODE版本上的_findfirst,以及UNICODE版本上的_wfindfirst

此外,还可以使用_tfinddata代替_finddata,后者还可以根据UNICODE配置解析为正确的结构

另一件事是,您还应该使用正确的文本,
\T(“C://*”
将在UNICODE构建中
L“C://*”
,否则
“C://*”
。如果您知道正在使用UNICODE定义构建,那么请使用
std::vector

顺便说一句,Visual Studio默认情况下将使用UNICODE创建项目,您可以只使用像
\u wfindfirst
这样的函数的广泛版本,因为没有很好的理由构建非UNICODE项目

TCHAR和我理解它是wchar_t*或char*取决于使用UTF-8字符集或多字节字符集(我目前使用UTF-8)


这是错误的,在UNICODE windows API中使用UTF-16。
sizeof(wchar\u t)==2

\u TCHAR
是应该在“windows.h”中定义的宏。是否包含此标头?我正在使用visual studio 2015和预编译标头,因此我猜它要么更改为_-tfindfirst/_-tfinddata\u t,要么完全删除TCHAR?visual studio将使用TCHAR.h生成代码,因此为了保持一致,最好使用TCHAR.h。您可以在此处找到这些映射:。但在您的代码中,您可以使用w函数的ide版本-代码将被编译。我个人使用TCHAR.h.@Arrrow实际上你的代码应该“神奇地”一旦您在项目设置中更改:General->Character Set为“Use Multi-Byte Character Set”(使用多字节字符集),就开始工作。但我建议您继续使用UNICODE并修复您的代码。这与常规std::string的作用相同吗?@Arrrow--这就是此模板的优点--是的,它的工作方式应该与
std::string
完全相同。为什么不试试看呢?
vector<TCharString> dirArray;