By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

CString process '\0' in the middle of string

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
dave wrote:
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.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.