C++ TCHAR[] 转字符串
我有这个方法,它通过 TCHAR szFileName[]
变量接收路径,其中包含类似 C:\app\...\Failed\
我想对它进行排序,以便我可以验证该路径上最后一个文件夹的名称是否实际上是“失败”
我认为使用类似的东西会起作用:
std::wstring Path = szFileName;
string dirpath2;
dirpath2 = Path.substr(0,5);
但我收到以下错误:
错误 6 错误 C2679:二进制“=”:否 发现运算符需要一个 类型的右侧操作数 'std::basic_string<_Elem,_Traits,_Ax>' (或者没有可接受的 转换)
不用说,我对 C++ 很陌生,我一直在寻找答案一段时间了,但我没有任何运气,所以任何帮助将不胜感激:)
I have this method which receives a path through a TCHAR szFileName[]
variable, which contains something like C:\app\...\Failed\
I'd like to sort through it so I can verify if the name of the last folder on that path is in fact, "Failed"
I thought that using something like this would work:
std::wstring Path = szFileName;
string dirpath2;
dirpath2 = Path.substr(0,5);
But I get the following error:
Error 6 error C2679: binary '=' : no
operator found which takes a
right-hand operand of type
'std::basic_string<_Elem,_Traits,_Ax>'
(or there is no acceptable
conversion)
Needless to say, I'm very new to C++, and I've been looking for an answer for a while now, but I haven't had any luck, so any help would be appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您要么始终使用
wstring
(宽字符变体),要么使用string
(“正常”变体)。由于您得到的是
TCHAR
(可以是wchar_t
或char
,具体取决于编译器标志),因此要使用的适当类型是一个tstring
,但它不存在。但是,您可以为其定义一个 typedef:现在您可以在所有操作中一致使用相同的字符串类型
tstring
。Either you’re consistently using
wstring
(the wide character variant) orstring
(the “normal” variant).Since you’re getting a
TCHAR
(which can be eitherwchar_t
orchar
, depending on compiler flags), the appropriate type to use would be atstring
, but that doesn’t exist. However, you can define a typedef for it:Now you can consistently use the same string type,
tstring
, for all your operations.dirpath2
也必须是std::wstring
。有多种方法可以在两者之间进行转换,但它们涉及更改字符编码,这似乎超出了您的要求。我喜欢干脆不使用 TCHAR。如今,很少需要启用或禁用 UNICODE 宏以及创建程序的 ASCII 和 Unicode 版本。请始终使用
wstring
、wchar_t
以及以“W”结尾的 Windows API 函数。如果您正在从事无法控制上述内容的工作,Konrad 的
typedef
答案比我的更实用。dirpath2
has to be astd::wstring
as well. There are ways to convert between the two, but they involve changing the character encoding and that seems like more than you're asking for.I like to simply not use TCHAR. Today, there is rarely is there a need to enable or disable the UNICODE macros and create both an ASCII and a Unicode version of a program. Just always use
wstring
,wchar_t
, and the Windows API functions that end in 'W'.If you're working on something where you don't have control over the above, Konrad's
typedef
answer is more practical than mine.