Solved

Just trying to 'cout' a CString

Posted on 2006-06-15
19
2,533 Views
Last Modified: 2013-12-14
Excuse the stupidity of this question, but its been bugging me:

<<CODE>>
   CString blah("HELLO");
   cout << blah << endl;
   printf ("%s", blah);
<</CODE>>

In the 'cout' case it seems to be printing out the mem address of 'blah', in the 'printf' case it prints out only the first letter of 'blah'  ("H").  What the heck is going on here.  Any help is appreciated....Im sure this is an easy one, I'm just new to CStrings.  Thanks
0
Comment
Question by:Pheonix911
  • 6
  • 5
  • 5
  • +2
19 Comments
 
LVL 4

Expert Comment

by:MikeGeig
ID: 16912808
Are you attached to the CString class? I would recommend using the std::string class instead. string is the newer more c++ based version of the c based CString.

the code you want would looke something like

string blah = "hello";
cout << blah;
0
 
LVL 4

Expert Comment

by:MikeGeig
ID: 16912829
i forgot

printf(%s, blah);

but thats not exactly vital since it didn;t change between to two. Also, the new string.h is an awesome class because it contains many new methods to add easy use to your programs.
0
 

Author Comment

by:Pheonix911
ID: 16912971
ok...but saying I wanted to use CString...your saying I can't???
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
ID: 16913108
>>ok...but saying I wanted to use CString...your saying I can't???

You can use CString, but you should cast it to the LPCTSTR operator.

cout << (LPCTSTR)blah << endl;

If you don't like cast, you can do the following:
cout << blah.operator LPCTSTR() << endl;
0
 
LVL 86

Expert Comment

by:jkr
ID: 16913111
Try

cout << (LPCTSTR) blah << endl;

which will call 'CString::operator LPCTSTR()'. This way, you'll get the contents as a 'char*', wich works with 'cout'.
0
 
LVL 30

Expert Comment

by:Axter
ID: 16913155
By the way, my personal preference is CString over std::string.

Most hard-core C++ programmers prefer std::string over CString.

There's nothing wrong with mixing CString and STL code.
For example, I prefer std::vector over CArray, so I use std::vector<CString> instead of CStringArray.

You'll also find CString to be more efficient then the std::string implementation that comes with VC++.  At least I know this to be true with VC++6.0.  I'm not sure if they've improved the std::string implementation in 7.1 or 8.0
0
 
LVL 30

Expert Comment

by:Axter
ID: 16913209
MikeGeig,
>> Also, the new string.h is an awesome class because it contains many new methods to add easy use to your programs.

There is no new string.h header for C++.
The std::string class is declared in the <string> header.

The official C++ standard has a string.h header which is the C string.h header, and does not include std::string declaration.

0
 
LVL 4

Expert Comment

by:MikeGeig
ID: 16913328
axter, that was a mistake on my part, after reading it, i did not intend it the way it seems. I just meant the string class in general.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 16913477
Here's a fix for ostream.

--------8<--------
// Compile with /MT /EHsc

#include "afx.h"
#include <iostream>

#if 1200 <= _MSC_VER /* For the sake of VC 6. VC 7 doesn't need this */
inline std::ostream& operator<<(std::ostream& os,const CString& str)
{
      return os << static_cast<LPCTSTR>(str);
}
#endif


int main()
{
      CString greeting = "Hello, world";
      std::cout << greeting << std::endl;
      return 0;
}
--------8<--------
0
 
LVL 30

Expert Comment

by:Axter
ID: 16913800
>>inline std::ostream& operator<<(std::ostream& os,const CString& str)

Good idea, but I would not use the static_cast, and instead call the operator directly.

inline std::ostream& operator<<(std::ostream& os,const CString& str)
{
     return os << str.operator LPCTSTR();
}
0
 

Author Comment

by:Pheonix911
ID: 16919533
ok...so it sounds like I should be using std::string.  But the reason I was using CString was to ease my casting from wchar_t.  How can I convert wchar_t to std::string?
0
 
LVL 4

Expert Comment

by:MikeGeig
ID: 16919552
0
 
LVL 4

Expert Comment

by:MikeGeig
ID: 16919562
0
 
LVL 30

Expert Comment

by:Axter
ID: 16920001
>>ok...so it sounds like I should be using std::string.  But the reason I was using CString was to ease my casting from wchar_t.  How can I convert wchar_t to std::string?

Beaware that a disadvantage of using std::string over CString is that your code will not work implicitly with API functions that take TCHAR.
So if you ever decide to change your project from ANSI to UNICODE or from UNICODE to ANSI, you're going to have to make a lot of code changes.
Another disadvantage, is that the VC++ std::string is slower then CString.
Also, std::string doesn't work implicitly with printf type functions like CString does, so you'll have to use string::c_str function when using it with printf, or any function that takes a C-string pointer.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 16920247
> How can I convert wchar_t to std::string?

Use std::wstring, std::wcout, std::wcin, std::wfstream etc, if you want to use wide characters predominantly in your application. There are wchar_t counterparts for all of the standard library char classes.
0
 

Author Comment

by:Pheonix911
ID: 16922373
    So I've modified my code like so:

<<CODE>>
  string blah = "hello";
  cout << (LPCTSTR)blah;
<</CODE>>

     And heres the weird thing.  In VS .Net 2k5 I get the mem location of 'blah'.  However in VS .Net 2003 I get the correct output 'hello'.  Whoever gets this, gets the points :o)  As this as been killing be for far too long.
Thanks
0
 

Author Comment

by:Pheonix911
ID: 16922408
That is supposed to be:

<<CODE>>
  CString blah = "hello";
  cout << (LPCTSTR)blah;
<</CODE>>

sorry
0
 
LVL 30

Expert Comment

by:Axter
ID: 16922531
Try using the operator function as I previously posted.
cout << blah.operator LPCTSTR() << endl;
0
 

Author Comment

by:Pheonix911
ID: 16923060
The answer was actually that VS.Net 2005 is set, by default to unicode...where CStrings are multibyte's ... changing this in the project properties resolved the issue....although I still needed to type case, so giving points to Axter...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

Suggested Solutions

Title # Comments Views Activity
GUI: DIalog Stacking and Popping in MS C++ 4 76
How to convert MFC::CString to UTF8 wchar_t* 10 214
Create a path if not exists 7 78
Angular JS Route 3 71
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

821 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