简体   繁体   中英

Concatenating strings of different types in C++

How can I concatenate the following char and TCHAR variables in C++?

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. I need to end up with fileName being something like file_randomIdGoesHere.csv

I have tried 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 * .

Any ideas?

The error I get with strncat(fileName, id, 10) is 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.

In the context of Windows C++ programming, CString is a very convenient string class.
You can use its overloaded operator+ (or += ) to concatenate strings in a convenient, robust and easy way.

If you have an id stored in a char string (as an ASCII string), as you showed in your question's code:

 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):

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 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* ).

To use CString, you can simply #include <atlstr.h> .

The error you are seeing is because your id array is declared wrong. 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. So you may as well replace TCHAR with 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() :

_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 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:

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[ ]? )

Then, concatenate two TCHAR strings using _tcscat().

If you are not using UNICODE character table. Than your TCHAR is equivalent to 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM