问题描述
如何打印LPCTSTR的阵列
How to print array of LPCTSTR
例如
LPCTSTR pszValueNames[] = { L"partnerID", L"partnerSubID", L"transactionID" };
for (int i = 0; i < (sizeof(pszValueNames) / sizeof(LPCWSTR)); i++)
{
cout << (*pszValueNames[i]) << endl;
}
上面给出的一些数字是不是真实的LPCTSTR值。
当我使用的wchar_t *和所有其他基本类型它吐良好的价值观。
Above give some numbers which is not real lpctstr values.When i use wchar_t* and all other basic types it spit good values.
推荐答案
为什么你打印的地址是的std :: COUT
适用于<$ C $的原因C>的std ::字符串,它是基于字符
。一个 LPCWSTR
是一个宽字符串指针类型,而 COUT
不知道如何来显示宽字符串字符串。
The reason why you get the address printed is that std::cout
works with std::string
, which is char
based. An LPCWSTR
is a wide string pointer type, and cout
has no idea how to display wide strings "as strings".
您应该使用的std :: wcout
来处理宽字符串。
You should use std::wcout
to handle a wide string.
的std :: wcout&LT;&LT; pszValueNames [1] - ;&下;的std :: ENDL
;
另外,你的 LPCTSTR
的使用是不正确的,即使你的程序可能会工作。如果你知道你正在使用宽字符串,指定 LPCWSTR
的任何地方,你会使用 LPCTSTR
。其原因是, LPCTSTR
不一定是一个宽字符串指针类型,取决于版本(MBCS或统一code)对所使用的类型。
Also, your usage of LPCTSTR
is not correct, even though your program may work. If you know you're using wide strings, specify LPCWSTR
anywhere you would have used LPCTSTR
. The reason is that LPCTSTR
is not necessarily a wide string pointer type, depending on the type of build (MBCS or Unicode) that you are using.
所以,简单地说,你的字符串的指针声明是不同的字符串类型的大杂烩,一些功能可能工作(如 LPCTSTR
是一个非宽字符指针),而其他的字符串处理函数将无法正常工作。
So in a nutshell, your string pointer declarations are a hodge-podge of different string types, where some functions may work (if the LPCTSTR
is a non-wide char pointer), while other string handling functions won't work.
基本上,如果你要使用 LPCTSTR
或 LPTSTR
,那么所有的code的处理字符串或字符串指针类型应该使用集结中性类型,即使用 LPCTSTR
, TCHAR
, LPTSTR
, _T()
等,如果从MBCS去建立一个统一$ C $这些类型将改变无缝ç版本(反之亦然无论何种原因)。
Basically, if you're going to use LPCTSTR
or LPTSTR
, then all of your code that handles string or string pointer types should use "build-neutral" types, i.e. use LPCTSTR
, TCHAR
, LPTSTR
, _T()
, etc. These types will change seemlessly if you go from an MBCS build to a Unicode build (and vice-versa for whatever reason).
如果在另一方面,如果你知道生成将永远只能是的Uni code
,那么 LPCWSTR
和 LPWSTR
应该在你的程序中使用。相反,如果你知道并能保证构建只会MBCS,然后用 LPCSTR
和 LPSTR
在你的程序
If on the other hand, if you know that the build will only ever be Unicode
, then LPCWSTR
and LPWSTR
should be used throughout your program. Conversely, if you know and can guarantee that the build will only be MBCS, then use LPCSTR
and LPSTR
in your program.
做的最好的事情就是要么使用集结中立类型( LPCTSTR
等),整个应用程序,或所有这些改变为宽字符串类型( LPCWSTR
等)。该MBCS版本是成为这些天非常罕见的,所以还不如开始使用宽字符串类型的开发,并且只能使用字符
应用程序中的字符串类型,当你的接口给其他依赖于字符
函数基于字符串。
The best thing to do is either use the build-neutral types (LPCTSTR
, etc.) throughout your application, or change all of these to wide string types (LPCWSTR
, etc.). The MBCS builds are becoming very rare these days, so might as well start developing using wide string types, and only use char
string types in your application when you're interfacing to other functions that rely on char
based strings.
这篇关于如何打印C ++ LPCTSTR的阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!