how can concatenate following char
, 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 generates random string. need end filename
being file_randomidgoeshere.csv
i have tried strncat(filename, prefix, 5);
works fine tchar
variables not char *
requires const char *
instead, maybe there's better way of doing it, not sure how convert char *
or char **
const char *
.
any ideas?
the error strncat(filename, id, 10)
error: cannot convert 'char**' 'const char*'
the first thing should is, since using c++ , not pure c, use string class represent strings , manage them in way more convenient raw c-style character arrays.
in context of windows c++ programming, cstring
convenient string class.
can use overloaded operator+
(or +=
) concatenate strings in convenient, robust , easy way.
if have id stored in char string (as ascii string), showed in question's code:
char id[10]; generateid(id);
you can first create cstring around (this convert char-string tchar-string, in particular wchar_t-string if using unicode builds, have been default since vs2005):
const cstring strid(id);
then, can build whole file name string:
// // build file name using format: // // file_<generatedidgoeshere>.csv // cstring filename(_t("file_")); filename += strid; filename += _t(".csv");
as alternative, use cstring::format
method, e.g.:
cstring filename; filename.format(_t("file_%s.csv"), strid.getstring());
you can pass instances of cstring lpctstr parameters in win32 apis, since cstring offers implicit conversion lpctstr (i.e. const tchar*
).
to use cstring, can #include <atlstr.h>
.
Comments
Post a Comment