# Is there a way to use std::cout with a LPTSTR?

This topic is 4665 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to output a string to the console, where the string came from a function that returned a LPTSTR. So basically:
LPTSTR  burp = _T("Fffurrp!");
std::cout << (CHAR*)burp << endl;
This only outputs a single letter, 'F'. Anyone know how to get the whole string to print?

##### Share on other sites
Sounds like you're compiling in Unicode mode, where LPTSTR represents strings in UTF-16 format. The second byte of your data, then, is zero, and gets interpreted as the end of the string by std::cout. Try std::wcout instead:
LPTSTR burp = _T("Fffurrp!");std::wcout << burp << std::endl;

##### Share on other sites
Wahey that works.. but the 'w' functions only handle Unicode right? I've been using the 't' conversions and functions so it can handle ascii, unicode, or multibyte characters as well.

I can't find something like "tcout". With sprintf you have a _stprintf, which just uses sprintf for multibyte characters anyway. Just that cout I need.

##### Share on other sites
You'll need to define it yourself. The C++ Standard library components don't come with generic text mapping aliases.

##### Share on other sites
#ifdef UNICODEostream& tcout = wcout;#elseostream& tcout = cout;#endif

Should work (and without evil #define) :)

EDIT: Yes, it was the wrong way around before. Doh :)

[Edited by - Zahlman on January 4, 2006 4:28:16 PM]

##### Share on other sites
Ah, thank you. Should have thought of defining my own one sooner.
But now I'm curious about these new things mentioned... #define is evil? Actually I'm getting linker errors using the ostream& way.. using #define does get it to compile.

And what is the difference between UNICODE and _UNICODE? (I think you got your cout and wcout around the wrong way there too.)

##### Share on other sites
The difference between UNICODE and _UNICODE is mostly historical. All well behaved programs that define either should define both.

##### Share on other sites
To be completely honest, I don't think there's any point in using T-strings these days. T-strings seem primarily designed to allow you to write for Unicode while still allowing you to compile a separate ANSI-string version for the older Windows versions that don't have sufficient Unicode support (95, 98 and ME). However, since the Microsoft Layer for Unicode was released, your Unicode version will work on the older Windows versions as well. I'd rather just assume Unicode support and be done with it. :-)

##### Share on other sites
Quote:
 Original post by DefendAh, thank you. Should have thought of defining my own one sooner.But now I'm curious about these new things mentioned... #define is evil? Actually I'm getting linker errors using the ostream& way.. using #define does get it to compile.

#define should not be used when there is a way to accomplish the same goal using compiler constructs, and not preprocessor constructs. Consider the case of using #define TWO 2 vs. const int TWO = 2. They accomplish nearly the same goal but the latter is definately better.

I wouldn't go so far as to say #define is always evil...it has its uses.

##### Share on other sites
evil (but do understand "evil").

As for the reference, I don't know why it wouldn't be working for you. It looks OK to me but I've had problems trying to work with ostream references before... :(

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632984
• Total Posts
3009713
• ### Who's Online (See full list)

There are no registered users currently online

×