473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

CString will not go to cout

Expand|Select|Wrap|Line Numbers
  1. CString st1;
  2. st1.Format(_T("%s"),"010210002022E803E903"); 
  3. printf_s("%s\n",st1);//output:010210002022E803E903
  4. //wcout<<st1.GetString()<<endl;            //error,why? 
  5.  
  6.  
  7.  
  8. CString st=_T("010210002022E803E903");
  9. printf_s("%s\n",st);//output:0,why?
  10. wcout<<st.GetString()<<endl;//output:010210002022E803E903 
in Visual Studio 2008
Jul 20 '09 #1

✓ answered by Banfa

CString reference (admittedly from Visual C++ 6.0 but getting at any CString documentation is hard nowa days as MS seem to have deleted all the links to its documentation).

CString is based on the TCHAR data type.
And therefore CString is not ASCII only although I was surprised when I found this out about a week ago.

12 7455
Banfa
9,065 Expert Mod 8TB
Because %s in printf (and similar functions) indicates a parameter on the stack that points to a c string, that is a zero terminated array of characters. Your variable, st is an object of type CString not a pointer to a c string. You can however cast it to a constant pointer to a c string

(LPCSTR)st

because the CString object implements an operator for this so

printf_s("%s\n",(LPCSTR)st);

should work for you.
Jul 20 '09 #2
Expand|Select|Wrap|Line Numbers
  1. CString st=_T("010210002022E803E903"); 
  2. printf_s("%s\n",(LPCTSTR)st);
I've tried this method.But I still get the output:0
Jul 21 '09 #3
Banfa
9,065 Expert Mod 8TB
Sorry the other thing is that CString deals with what ever character type Windows is using. This is normally unicode.

printf (and related functions) however still use a straight ASCII string.

To printf a CString you will need to convert the characters from Unicode to ASCII.

There should be plenty of information about this available either through google or msdn.
Jul 21 '09 #4
Thank you very much for your help,but I pulzzed
Expand|Select|Wrap|Line Numbers
  1. CString st1; 
  2. st1.Format(_T("%s"),"010210002022E803E903"); //output:010210002022E803E903  
  3. printf_s("%s\n",st1);
st1 has a string of Unicode,but,printf_s alao can output in correct.
Jul 21 '09 #5
JosAH
11,448 Expert 8TB
I didn't even know the ostream class had an overloaded << operator for that CString class; why don't you use the string class? Every implementation understands that.

kind regards,

Jos
Jul 21 '09 #6
Banfa
9,065 Expert Mod 8TB
@JosAH
It doesn't have an overload operator for CString, the wcout call is made on the return of a CString member function that returns a pointer to the CStrings buffer.

I assume that if the OP is using a CString then they are using MFC in general. In my experience MFC classes do not play particularly nicely with classes from other libraries including the stl.
Jul 21 '09 #7
Banfa
9,065 Expert Mod 8TB
@evenstar
It isn't clear if you mean "printf_s also outputs it correctly" or "printf_s outputs it incorrect" which have opposite meanings.

Try calling wprintf_s, the wide character version of printf_s or use the format specifier %ls or %ws both of which indicate wide character strings.
Jul 21 '09 #8
sorry,I mean printf_s also outputs it correctly

CString st1;
st1.Format(_T("%s"),"010210002022E803E903");
printf_s("%s\n",st1); //output:010210002022E803E90

CString st2;
st2=_T("010210002022E803E903");
printf_s("%ws\n",st2); //also output:010210002022E803E90

CString st3;
st3=_T("010210002022E803E903");
printf_s("%s\n",st2); //output:0


I think there are some differents between "="operator and "format()".
"st2=_T("010210002022E803E903");" will make string be saved as "%ws" in st2? So "printf_s("%s\n",st2) "only output 0,I guessed.
And I find another problem that
Expand|Select|Wrap|Line Numbers
  1. int _t main(int argc, _TCHAR* argv[])
  2. {
  3. CString st1;  
  4. st1.Format(_T("%s"),"010210002022E803E903");  
  5. printf_s("%ws\n",st1);
  6.  
  7. return 0;
  8. }
only output:"pess any key to continue..."
there is no compile error or runtime error,but the code is not be executed
Jul 24 '09 #9
weaknessforcats
9,208 Expert Mod 8TB
No one has mentioned that the OP code is not TCHAR compliant.

That is, in TCHAR code you cannot use standard C functions directly since the program must be able to switch between ASCII and Unicode by means of a compiler switch.

That means all of your functions must switch automatically. The ASCII strlen must change to the Unicode wcslen without making any program changes.

That means you must use the TCHAR macros.

Which you are not. Hence, the garbage.

_tprintf is the TCHAR macro you want.

BTW: CString is ASCII only. That means you should not be using this directly in TCHAR programs. Instead, use PTCHAR only. Then assign to this pointer the results of a macro that on the ASCII side is a PCHAR from CString and on the Unicode side is a PWCHAR obtained by converting your CString into a LPWSTR string.

Personally, I never use CString. Instead I use the C++ string and wstring:
Jul 27 '09 #10
Banfa
9,065 Expert Mod 8TB
CString reference (admittedly from Visual C++ 6.0 but getting at any CString documentation is hard nowa days as MS seem to have deleted all the links to its documentation).

CString is based on the TCHAR data type.
And therefore CString is not ASCII only although I was surprised when I found this out about a week ago.
Jul 27 '09 #11
Thank you all for your attention
Jul 28 '09 #12
weaknessforcats
9,208 Expert Mod 8TB
And therefore CString is not ASCII only although I was surprised when I found this out about a week ago.

I am surprised too. I did not know MSDN supported TCHAR. I can see this TCHAR support was added after thge original release since several code examples where methods use LPCTSTR don't work since ASCII is hard-coded in the examples. This tells me MS just tested with ASCII and not UNICODE.

I wonder how MS gets this to work with libraries where CString is compiled with UNICODE undefined and the library is used in an ASCII program. You need two libraries?
Jul 28 '09 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Rajesh Garg | last post by:
Why and in what situations is CString not preferred? RVG
6
by: Markus Hämmerli | last post by:
I ' ll tra to convert a Cstring to char* without success. I working in a Unicode enabled environment this is working in Unicode CString source = _T("TestString"); TCHAR *szSource =...
8
by: Oskar | last post by:
Hi. I`m new in cpp and i have a litlle problem. i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323 dvjiv 234") and i want to put the data (space separated) into an array.It shuld...
5
by: Tim Wong | last post by:
All: I am trying to convert a CString value to an unsigned char array. I found some code online that will allow me to compile, but when I try to print out...i get a whole mess. /*Begin Code*/...
4
by: Cactus | last post by:
How to convert unsigned char* to CString: I wrote some function: u_char_ =55; u_char_ =66; u_char_ =77; .........=ii...... Convert_to_CS(u_char_);
5
by: Randeh | last post by:
Short story: in a beginning C++ class in college, was in a car accident that caused central spinal stenosis, some new Schmorl's nodes, disc problems and an incredible amount of pain. So far I've...
9
by: Donos | last post by:
I have a CString with 100 characters. Now i want to make that to 2 lines. For example, CString str = "This is just a test to find out how to break a cstring"; I want this CString in the...
3
by: Carl Forsman | last post by:
how to compare a CString? the following does not seem to work. CString id = PictureEntries.attribute("id").value(); if (id.Compare("2222") == 1){ cout << id << endl; }
9
by: kylie991 | last post by:
Hi there just wondering if I am on the right track. I need to load a file in a program and then store the stata in an array. I am now testing this function to see if it is working... when I run the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.