Solved

'cout' a 'CString'

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

777 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