C++ TCHAR[] 转字符串

发布于 2024-09-27 14:00:29 字数 500 浏览 2 评论 0原文

我有这个方法,它通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩物 2024-10-04 14:00:29

您要么始终使用 wstring (宽字符变体),要么使用 string (“正常”变体)。

由于您得到的是 TCHAR (可以是 wchar_tchar,具体取决于编译器标志),因此要使用的适当类型是一个tstring,但它不存在。但是,您可以为其定义一个 typedef:

typedef std::basic_string<TCHAR> tstring;

现在您可以在所有操作中一致使用相同的字符串类型 tstring

Either you’re consistently using wstring (the wide character variant) or string (the “normal” variant).

Since you’re getting a TCHAR (which can be either wchar_t or char, depending on compiler flags), the appropriate type to use would be a tstring, but that doesn’t exist. However, you can define a typedef for it:

typedef std::basic_string<TCHAR> tstring;

Now you can consistently use the same string type, tstring, for all your operations.

獨角戲 2024-10-04 14:00:29

dirpath2 也必须是 std::wstring 。有多种方法可以在两者之间进行转换,但它们涉及更改字符编码,这似乎超出了您的要求。

我喜欢干脆不使用 TCHAR。如今,很少需要启用或禁用 UNICODE 宏以及创建程序的 ASCII 和 Unicode 版本。请始终使用 wstringwchar_t 以及以“W”结尾的 Windows API 函数。

如果您正在从事无法控制上述内容的工作,Konrad 的 typedef 答案比我的更实用。

dirpath2 has to be a std::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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文