For Programmers: Free Programming Magazines  


Home > Archive > VC Language > November 2005 > String Manipulations









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author String Manipulations
Mark

2005-11-22, 7:04 pm

Can anyone provide information for how to retrieve the actual character
string from a CString object?

I am using the following code but it only returns the address of the string
and not the literal characters of the string. I know it has to be something
simple that I am overlooking but I have copied samples from MS that are
supposed to work but give the same results

CString strSports("Hockey is Best!");

TCHAR sz[1024];

lstrcpy(sz, strSports);

cout << strSports ;

If I follow documentation provided by Microsoft which states:
// while this line will put the content of the string out:

cout << (LPCTSTR) strSports;

I still only receive the address of the CString object at the console.

Please help!


Igor Tandetnik

2005-11-22, 7:04 pm

Mark <mark_ivey4@yahoo.com> wrote:
> Can anyone provide information for how to retrieve the actual
> character string from a CString object?


CString str;
LPCTSTR p = str;

> I am using the following code but it only returns the address of the
> string and not the literal characters of the string. I know it has to
> be something simple that I am overlooking but I have copied samples
> from MS that are supposed to work but give the same results
>
> CString strSports("Hockey is Best!");
>
> TCHAR sz[1024];
>
> lstrcpy(sz, strSports);
>
> cout << strSports ;


Why did you copy into sz buffer if you never use it?

> If I follow documentation provided by Microsoft which states:
> // while this line will put the content of the string out:
>
> cout << (LPCTSTR) strSports;
>
> I still only receive the address of the CString object at the console.


It appears that you are building a Unicode build. If so, use wcout
instead of cout.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Mark

2005-11-22, 7:04 pm

Thanks Igor,

I get it. But I didn't explicitly designate my program to run under Unicode
although after taking your advice it worked as I originally expected. I am
using VC++ 2005, where would the program be designated to run under Unicode?

I also noted that there is another procedure I acquired from MS within the
same scope of the CString example that wont compile either.

sprintf(sz, "I think that %s!\n", (LPCTSTR) strSports);

The compiler doesn't like this and produces an error:

error C2664: 'sprintf' : cannot convert parameter 1 from 'TCHAR [1024]' to
'char *'

Perhaps my project environment needs to be adjusted a bit? Running sample
code shouldn't be this cumbersome.

Thanks


"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:%2304GFb57FHA.3544@TK2MSFTNGP09.phx.gbl...
> Mark <mark_ivey4@yahoo.com> wrote:
>
> CString str;
> LPCTSTR p = str;
>
>
> Why did you copy into sz buffer if you never use it?
>
>
> It appears that you are building a Unicode build. If so, use wcout instead
> of cout.
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925
>
>



Igor Tandetnik

2005-11-22, 7:04 pm

Mark <mark_ivey4@yahoo.com> wrote:
> I get it. But I didn't explicitly designate my program to run under
> Unicode although after taking your advice it worked as I originally
> expected. I am using VC++ 2005, where would the program be designated
> to run under Unicode?


I don't have VC2005 handy to check. In VC2003, that would be Project
Properties | General | Character Set = Use Unicode. I expect VC2005 to
be the same or very close.

> I also noted that there is another procedure I acquired from MS
> within the same scope of the CString example that wont compile either.
>
> sprintf(sz, "I think that %s!\n", (LPCTSTR) strSports);
>
> The compiler doesn't like this and produces an error:
>
> error C2664: 'sprintf' : cannot convert parameter 1 from 'TCHAR
> [1024]' to 'char *'


Make it

_stprintf(sz, _T("I think that %s!\n"), (LPCTSTR) strSports);

That should compile in both Unicode and Ansi builds. _stprintf is a
macro expanding to swprintf or sprintf correspondingly.

> Perhaps my project environment needs to be adjusted a bit? Running
> sample code shouldn't be this cumbersome.


Where are you getting this sample code? It does not seem to be
particularly well designed, to put it mildly.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Mark

2005-11-22, 7:04 pm

Thanks much Igor,

I would tend to agree with you based on the fact that the information you've
provided in just a few short replies being far more enlightening than the
sample code I acquired from this link:

http://msdn.microsoft.com/library/d...tor_lpctstr.asp

I am good on this now and Hope to chat again soon!

Mark

"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:OFy7S857FHA.3660@TK2MSFTNGP09.phx.gbl...
> Mark <mark_ivey4@yahoo.com> wrote:
>
> I don't have VC2005 handy to check. In VC2003, that would be Project
> Properties | General | Character Set = Use Unicode. I expect VC2005 to be
> the same or very close.
>
>
> Make it
>
> _stprintf(sz, _T("I think that %s!\n"), (LPCTSTR) strSports);
>
> That should compile in both Unicode and Ansi builds. _stprintf is a macro
> expanding to swprintf or sprintf correspondingly.
>
>
> Where are you getting this sample code? It does not seem to be
> particularly well designed, to put it mildly.
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925
>
>



David Webber

2005-11-23, 3:59 am


"Mark" <mark_ivey4@yahoo.com> wrote in message
news:esKypP67FHA.1032@TK2MSFTNGP11.phx.gbl...

> I would tend to agree with you based on the fact that the
> information you've provided in just a few short replies being far
> more enlightening than the sample code I acquired from this link:
>
> http://msdn.microsoft.com/library/d...tor_lpctstr.asp
>


That example is horrible. Whoever wrote it seems to have really no
understanding of the character types and functions.

Basically:

1. if you're using traditional characters, you can use types

char CHAR LPCSTR LPSTR CStringA constant characters and
strings defined by '.' and "..." and a whole host of traditional
functions like strlen() strcat() sprintf()...


2. if you're using wide characters, replace these by

wchar_t WCHAR LPCWSTR LPWSTR CStringW L'.' L"...."
wcslen() wcscat() wsprintf()....


3. If you want a program which builds to the latter if UNICODE and
_UNICODE are defined,and to the former if not, then use

TCHAR LPCTSTR LPTSTR CString _T('.') _T("...") _tcslen()
_tcscat() _stprintf()

[I hope I have remembered the alternative names correctly -
normally I check them with F1 - but you get the idea.]

But above all use the 'normal' 'w' and 't' options consistently
(as is not done in the example you quoted). [Note however that
you can pass all the CString constructors either "normal" or "wide"
strings and they do the conversion based on the character set of
your locale.]

Dave
--
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com