Solved

'cout' a 'CString'

Posted on 2007-11-14
4
2,298 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

932 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now