Solved

'cout' a 'CString'

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

762 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

20 Experts available now in Live!

Get 1:1 Help Now