CString process '\0' in the middle of string

D

dave

Hi,

I would apprecaite the followinghelp;

I need to pass a TCHAR * s = foo*.* \0 *h\0" to CString operator, but
it will stop reading input at teh first \0, how do I work around this
??

Thanks
Dave
 
D

Dietmar Kuehl

dave said:
I would apprecaite the followinghelp;

I need to pass a TCHAR * s = foo*.* \0 *h\0" to CString operator,

There is no such thing as 'TCHAR' or 'CString' in standard C++.
That is, you have selected the wrong forum to post your article
to. If you had asked how to create a corresponding
'std::basic_string', the answer would be simple:

/**/ char const cliteral[] = "foo*.+ \0 *h\0";
/**/ std::string s1(begin(cliteral), (end(cliteral) - 1));
/**/ wchar_t const wliteral[] = L"foo*.+ \0 *h\0";
/**/ std::wtring s1(begin(wliteral), (end(wliteral) - 1));

with suitable definitions of 'begin()' and 'end()', e.g. these:

/**/ template <typename T, int sz>
/**/ T* begin(T(&a)[sz]) { return a; }
/**/ template <typename T, int sz>
/**/ T* end(T(&a)[sz]) { return a + sz; }

Note that the arrays are one character too big: string literals
contain an additional terminating null character. Thus, you
need to substract "1" from the end to obtain a proper past the
end iterator.

BTW, note that the type of string literals is 'char const*' or,
for wide characters, 'wchar_t const*' (note the "const"). The
reason you can assign a string literal to a pointer to non-const
characters is to provide backward compatibility. You should not
rely on this assignment and a good compiler should warn about it
in default mode.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top