Solved

'cout' a 'CString'

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
how to understand recursion 12 229
Which IDE to use to begin C++ training? 5 65
is twain_32.dll cmpatible with windows 10 ? 10 181
One named event, multiple event handlers 2 24
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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.

791 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