Solved

'cout' a 'CString'

Posted on 2007-11-14
4
2,383 Views
Last Modified: 2008-02-01
I'm debugging some existing code that heavily uses CSstrings  and can't figure out how to do a simple 'cout' of a CSstring in a VS2005 console program. An older solution listed here does not work:

http://www.experts-exchange.com/Programming/Languages/CPP/Q_21887736.html?sfQueryTermInfo=1+cout+cstring

It says to cast like this:

CString blah("this is a CString");
cout << (LPCTSTR)blah << endl;

But that does not seem to work on Visual Studio 2005. It still outputs what looks like an address and not text. Here's some sample code:

#include <atlstr.h>
#include <iostream>
using namespace std;

void main (void)
{
      CString str("this is a CString");
      cout << (LPCTSTR)str << endl; // outputs numbers, not text
}
0
Comment
Question by:Fippy_Darkpaw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 20283390
Actually the snippet

void main (void)
{
      CString str("this is a CString");
      cout << (LPCTSTR)str << endl;
}

should work fine - what do you get when using

void main (void)
{
      CString str("this is a CString");
      cout << (char*)(LPCTSTR)str << endl;
}

?
0
 
LVL 4

Author Comment

by:Fippy_Darkpaw
ID: 20283507
The following prints an 8 digit number.

void main (void)
{
      CString str("this is a CString");
      cout << (LPCTSTR)str << endl;
}

Adding the (char*) as follows only prints a 't'

void main (void)
{
      CString str("this is a CString");
      cout << (char*)(LPCTSTR)str << endl;
}
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 20283528
Ah, you are using UNICODE? Then, that should be

void main (void)
{
      CString str("this is a CString");
      wcout << (LPCTSTR)str << endl; // 'wcout' instead of 'cout'
}
0
 
LVL 4

Author Comment

by:Fippy_Darkpaw
ID: 20283585
bah...it was the VS2005 'feature' where it defaults to Unicode Char set in every new project. Changed it to 'not set' and now it prints fine. Thanks. =)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question