• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2543
  • Last Modified:

Just trying to 'cout' a CString

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
Pheonix911
Asked:
Pheonix911
  • 6
  • 5
  • 5
  • +2
1 Solution
 
MikeGeigCommented:
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
 
MikeGeigCommented:
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
 
Pheonix911Author Commented:
ok...but saying I wanted to use CString...your saying I can't???
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
AxterCommented:
>>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
 
jkrCommented:
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
 
AxterCommented:
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
 
AxterCommented:
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
 
MikeGeigCommented:
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
 
rstaveleyCommented:
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
 
AxterCommented:
>>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
 
Pheonix911Author Commented:
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
 
MikeGeigCommented:
0
 
AxterCommented:
>>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
 
rstaveleyCommented:
> 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
 
Pheonix911Author Commented:
    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
 
Pheonix911Author Commented:
That is supposed to be:

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

sorry
0
 
AxterCommented:
Try using the operator function as I previously posted.
cout << blah.operator LPCTSTR() << endl;
0
 
Pheonix911Author Commented:
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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 6
  • 5
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now