- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我得到了以下代码:
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] 当前给出以下错误:
Error: argument of type "_TCHAR*" is incompatible with parameter of type "const char *"
现在我用谷歌搜索了 TCHAR,我知道它是 wchar_t* 还是 char*,具体取决于使用 Unicode 字符集还是多字节字符集(我目前使用的是 Unicode)。我也明白转换是一个巨大的痛苦。所以我要问的是:如何使用 _TCHAR 和 _findfirst 参数最好地解决这个问题?
我计划将 dirEntry.name 连接到 argv[1] 并在末尾连接一个“*”,并在另一个 _findfirst 中使用它。由于我仍在学习 C++,因此也欢迎对我的代码提出任何意见。
最佳答案
参见此处:_findfirst用于多字节字符串,而 _wfindfirst
适用于宽字符。如果您在代码中使用 TCHAR,则使用 _tfindfirst
(宏)它将在非 UNICODE 上解析为 _findfirst,在 UNICODE 构建上解析为 _wfindfirst。
此外,使用 _tfinddata_t 而不是 _finddata_t,它也将根据 UNICODE 配置解析为正确的结构。
另一件事是你也应该使用正确的文字,_T("C:/*")
将是 L"C:/*"
在 UNICODE 版本上,和 "C:/*"
否则。如果你知道你正在构建定义的 UNICODE,那么使用 std::vector<std::wstring>
.
顺便说一句。默认情况下,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).
这是错误的,在 UNICODE windows apis 中使用 UTF-16。 sizeof(wchar_t)==2
.
关于C++ _findfirst 和 TCHAR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36920784/
我正在为系统中的编译错误而苦苦挣扎,这是代码 struct Strless : public binary_function { public : bool operator()(cons
#include #include using namesapce std; void main() { map Map; Map.insert(make_pare(L"DOG1",1))
我想为一个大单元添加一个小的附加选项,所以我不想处理大量代码。 TCHAR szTempFileName[MAX_PATH]; TCHAR lpTempPathBuffer[MAX_PATH]; in
我遇到了一个奇怪的问题。 我正在编译一个使用静态库 MyLib.lib 的 MFC 应用程序 (VS 2008)。它在 Debug模式下编译正常。但在 Debug Unicode 中显示错误。我在 M
我想将 TCHAR nameBuf[] 转换为 string。 TCHAR nameBuf[MAX_COMPUTERNAME_LENGTH + 2]; DWORD nameBufSize; nameB
我创建了一个类来测试我需要使用的一些功能。本质上,该类将获取传入字符串的深层拷贝,并通过 getter 使其可用。我正在使用 Visual Studio 2012。在项目设置中启用了 Unicode。
下面是我的代码: string szDllPath = "12345"; size_t size = szDllPath.length(); TCHAR* wArr = new TCHAR[size]
我最近了解到可以在代码开头定义的 ## 功能。我正在尝试编译以下代码: #include #include #include #include #include #define paste(
TCHAR* pszBackupPath; m_Edt_ExportPath.GetWindowText(pszBackupPath, dwcchBackupPath); StrTrim(pszBac
我尝试创建 2 个矩阵:1 个 char* 和 1 个 THAR*。但是对于 TCHAR* 矩阵而不是字符串,我得到了某种地址。怎么了? 代码: #include #include using n
这段代码: #include TCHAR example_function() { TCHAR example_tchar[10]; return example_tchar; } int
我有一个简单的代码,argv[1] 是 "Привет"。 #include #include #include #include int _tmain(int argc, TCHAR* ar
我试图理解以下代码: const TCHAR * portName = "COM15"; 我知道 TCHAR 是 Char(在 ANSI 中)或 wChar(在 Unicode 中),基本上是一个表示
我有这个方法,它通过 TCHAR szFileName[] 变量接收路径,其中包含类似 C:\app\...\Failed\ 我想对其进行排序,以便我可以验证该路径上最后一个文件夹的名称是否实际上是“
我很确定这是一个愚蠢的问题,但它让我发疯.. 如何在控制台上打印 TCHAR 数组? DWORD error = WSAGetLastError(); TCHAR errmsg[512]; int r
我有一个项目使用了两个第三方库,它们都在头文件中使用了 TCHAR。不幸的是,一个库被编译为多字节(称为库 a),另一个被编译为 Unicode(称为库 b)。 现在我的理解是 TCHAR 被预编译器
如何将 TCHAR 拆分为其他变量?示例: TCHAR comando[50], arg1[50], arg2[50]; Mensagem msg; _tcscpy(msg.texto, TEXT("
我用 Visual Studio 创建了一个 VC++ 控制台项目,它自动生成了这个函数: int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { .
我是 Windows 编程新手,在阅读 Petzold 的书后,我想知道: 使用 TCHAR 类型和 _T() 函数来声明字符串是否仍然是一种好习惯,或者我应该只使用 wchar_t 和 L"" 新代
有以下方法可以初始化空值。 TCHAR x[5] = {'\0'}; TCHAR x[5] = _T("\0"); 我想知道哪个是正确的? 最佳答案 使用前一个。 _T(...) 将根据项目设置生成
我是一名优秀的程序员,十分优秀!