[英]Concatenating strings of different types in C++
How can I concatenate the following char
and TCHAR
variables in C++? 如何在C ++中串联以下
char
和TCHAR
变量?
TCHAR fileName[50];
TCHAR prefix[5] = "file_";
TCHAR ext[4] = ".csv";
char *id[10];
generateId(*id);
The generateId(char *s)
function simply generates a random string. generateId(char *s)
函数只是生成一个随机字符串。 I need to end up with fileName
being something like file_randomIdGoesHere.csv
我需要与最终
fileName
是类似file_randomIdGoesHere.csv
I have tried strncat(fileName, prefix, 5);
我已经尝试过
strncat(fileName, prefix, 5);
which works fine with all TCHAR
variables but not with char *
as it requires a const char *
instead, so maybe there's a better way of doing it, not sure how to convert char *
or char **
to const char *
. 它对所有
TCHAR
变量都适用,但对char *
无效,因为它需要一个const char *
,所以也许有更好的方法,不确定如何将char *
或char **
转换为const char *
。
Any ideas? 有任何想法吗?
The error I get with strncat(fileName, id, 10)
is error: cannot convert 'char**' to 'const char*'
我用
strncat(fileName, id, 10)
得到的error: cannot convert 'char**' to 'const char*'
是error: cannot convert 'char**' to 'const char*'
The first thing you should do is, since you are using C++ and not pure C, just use a string class to represent your strings and to manage them in a way much more convenient than raw C-style character arrays. 您应该做的第一件事是,由于您使用的是C ++,而不是纯C,因此只需使用字符串类来表示您的字符串,并以比原始C样式字符数组方便得多的方式管理它们。
In the context of Windows C++ programming, CString
is a very convenient string class. 在Windows C ++编程的上下文中,
CString
是一个非常方便的字符串类。
You can use its overloaded operator+
(or +=
) to concatenate strings in a convenient, robust and easy way. 您可以使用其重载的
operator+
(或+=
)以方便,健壮和简单的方式连接字符串。
If you have an id stored in a char string (as an ASCII string), as you showed in your question's code: 如果您将ID存储在char字符串(作为ASCII字符串)中,如问题代码中所示:
char id[10]; generateId(id);
you can first create a CString around it (this will also convert from char-string to TCHAR-string, in particular to wchar_t-string if you are using Unicode builds, which have been the default since VS2005): 您可以首先在其周围创建一个CString(如果使用的是Unicode构建,则这也将从char-string转换为TCHAR-string,尤其是wchar_t-string,这是VS2005以来的默认设置):
const CString strId(id);
Then, you can build the whole file name string: 然后,您可以构建整个文件名字符串:
//
// Build file name using this format:
//
// file_<generatedIdGoesHere>.csv
//
CString filename(_T("file_"));
filename += strId;
filename += _T(".csv");
As an alternative, you could also use the CString::Format
method, eg: 另外,您也可以使用
CString::Format
方法,例如:
CString filename;
filename.Format(_T("file_%s.csv"), strId.GetString());
You can simply pass instances of CString to LPCTSTR parameters in Win32 APIs, since CString offers an implicit conversion to LPCTSTR (ie const TCHAR*
). 您只需在Win32 API中将CString的实例传递给LPCTSTR参数,因为CString提供了对LPCTSTR的隐式转换(即
const TCHAR*
)。
To use CString, you can simply #include <atlstr.h>
. 要使用CString,您只需
#include <atlstr.h>
。
The error you are seeing is because your id
array is declared wrong. 您看到的错误是因为
id
数组被声明为错误。 You declared an array of pointers instead of an array of characters. 您声明了一个指针数组,而不是一个字符数组。 It should be more like this:
应该更像这样:
char id[10];
generateId(id);
That being said, you are also assigning char
-based string literals to your TCHAR
arrays, which means you are not compiling your project for Unicode, otherwise such assignments would fail to compile. 话虽这么说,您还向
TCHAR
数组分配了基于char
的字符串文字,这意味着您没有针对Unicode编译项目,否则此类分配将无法编译。 So you may as well replace TCHAR
with char
: 因此,您也可以将
TCHAR
替换为char
:
char fileName[50] = {0};
char prefix[] = "file_";
char ext[] = ".csv";
char id[10] = {0};
generateId(id);
And then, you should change strncat()
to _snprintf()
: 然后,您应该将
strncat()
更改为_snprintf()
:
_snprintf(filename, 49, "%s%s.cvs", prefix, id);
If you really want to use TCHAR
then you need to change everything to TCHAR
, and use the TEXT()
macro for literals: 如果您真的想使用
TCHAR
则需要将所有内容更改为TCHAR
,并使用TEXT()
宏作为文字:
TCHAR fileName[50] = {0};
TCHAR prefix[] = TEXT("file_");
TCHAR ext[] = TEXT(".csv");
TCHAR id[10] = {0};
generateId(id);
__sntprintf(filename, 49, TEXT("%s%s.cvs"), prefix, id);
If you cannot change id
to TCHAR
then you will have to perform a runtime conversion: 如果无法将
id
更改为TCHAR
则必须执行运行时转换:
TCHAR fileName[50] = {0};
TCHAR prefix[] = TEXT("file_");
TCHAR ext[] = TEXT(".csv");
char id[10] = {0};
generateId(id);
#ifdef UNICODE
wchar_t id2[10] = {0};
MultiByteToWideChar(CP_ACP, 0, id, -1, id2, 10);
#else
char *id2 = id;
#endif
__sntprintf(filename, 49, TEXT("%s%s.cvs"), prefix, id2);
First, convert char to TCHAR (see How to convert char* to TCHAR[ ]? ) 首先,将char转换为TCHAR(请参阅如何将char *转换为TCHAR []? )
Then, concatenate two TCHAR strings using _tcscat(). 然后,使用_tcscat()连接两个TCHAR字符串。
If you are not using UNICODE character table. 如果您不使用UNICODE字符表。 Than your TCHAR is equivalent to char.
比您的TCHAR等同于char。
TCHAR prefix[6] = "file_"; //don't forget to allocate space for null terminator '\0'
TCHAR ext[5] = ".csv"; // size is not 4, remember null terminator
char id[10] = "random"; // no need to use char* here
std::ostringstream oss;
oss << prefix << id << ext << std::endl;
std::cout << oss.str() << std::endl; // gives you file_random.csv as output
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.