How to Convert CString to _TCHAR *
-
Hi all.. I want to convert CString to _TCHAR* ////////////////////////////// _TCHAR *sEndDate; CString CurrDate; ///////////////////// I am trying this code.. sEndDate = (LPCSTR)CurrentDate; Its not working ... plz help me manju
Hi.. I am Mnaju.I have Completed my B.E Computers Science.Lokking for a job.I am interested in VC++ manju
-
Hi all.. I want to convert CString to _TCHAR* ////////////////////////////// _TCHAR *sEndDate; CString CurrDate; ///////////////////// I am trying this code.. sEndDate = (LPCSTR)CurrentDate; Its not working ... plz help me manju
Hi.. I am Mnaju.I have Completed my B.E Computers Science.Lokking for a job.I am interested in VC++ manju
You could try, TCHAR* sEndDate = CurrDate.GetBuffer(1); but I highly recommend you don't use CString at all until you've read the source code for it and got an understanding of it.
"The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)
-
You could try, TCHAR* sEndDate = CurrDate.GetBuffer(1); but I highly recommend you don't use CString at all until you've read the source code for it and got an understanding of it.
"The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)
Please don't forget to mention that she will need to call
CString::ReleaseBuffer()
after making a call toCString::GetBuffer()
. You may think that it is obvious, but you are guiding a person here, who is confused about string conversions. I hope you get the point.Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
-
Please don't forget to mention that she will need to call
CString::ReleaseBuffer()
after making a call toCString::GetBuffer()
. You may think that it is obvious, but you are guiding a person here, who is confused about string conversions. I hope you get the point.Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
Hence my advice to read and understand the source code or not use CString at all. It's not much use saying she will need to call CString::ReleaseBuffer(), (which is not always true ) if she doesn't understand when she should and when she shouldn't. Rather than me trying to post an entire CString tutorial here I recommended reading the source as it's the only sure way to understand the hairy mess that is CString.
"The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)
-
Hi all.. I want to convert CString to _TCHAR* ////////////////////////////// _TCHAR *sEndDate; CString CurrDate; ///////////////////// I am trying this code.. sEndDate = (LPCSTR)CurrentDate; Its not working ... plz help me manju
Hi.. I am Mnaju.I have Completed my B.E Computers Science.Lokking for a job.I am interested in VC++ manju
manju#123 wrote:
sEndDate = (LPCSTR)CurrentDate; Its not working ... plz help me
the
(LPCSTR)CurrentDate
returns a constant TCHAR pointer. you can make it compilable by makingsEndDate
as const. For instance,const _TCHAR *sEndDate;
If you want to modify the
sEndDate
, then you can useGetBuffer()
as suggested by Matthew Faithfull, But dont forget to callReleaseBuffer()
. Regards, Jijo._____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
-
Hence my advice to read and understand the source code or not use CString at all. It's not much use saying she will need to call CString::ReleaseBuffer(), (which is not always true ) if she doesn't understand when she should and when she shouldn't. Rather than me trying to post an entire CString tutorial here I recommended reading the source as it's the only sure way to understand the hairy mess that is CString.
"The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)
OK - there's this extra layer of understanding that I have about the OP, since I've been interacting with her for quite sometime now. She will not be able to understand anything from CString source code. And that was the point behind me stating whatever to you.
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
-
OK - there's this extra layer of understanding that I have about the OP, since I've been interacting with her for quite sometime now. She will not be able to understand anything from CString source code. And that was the point behind me stating whatever to you.
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
No problem, in that case my recommendation not to use CString stands :)
"The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)
-
Hi all.. I want to convert CString to _TCHAR* ////////////////////////////// _TCHAR *sEndDate; CString CurrDate; ///////////////////// I am trying this code.. sEndDate = (LPCSTR)CurrentDate; Its not working ... plz help me manju
Hi.. I am Mnaju.I have Completed my B.E Computers Science.Lokking for a job.I am interested in VC++ manju
Hi experts... What about this?
_tcscpy(sEndDate,CurrentDate);
I am using this style. Is this wrong or right way?modified on Tuesday, May 27, 2008 7:42 AM
-
No problem, in that case my recommendation not to use CString stands :)
"The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)
Umm... I'll have to agree. But you're a tough guy with that principle man. One must either know what CString is, or should not use it at all. :-D
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
-
Hi experts... What about this?
_tcscpy(sEndDate,CurrentDate);
I am using this style. Is this wrong or right way?modified on Tuesday, May 27, 2008 7:42 AM
That makes a copy. It is fine sometimes (and the other times it is wrong). :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Hi experts... What about this?
_tcscpy(sEndDate,CurrentDate);
I am using this style. Is this wrong or right way?modified on Tuesday, May 27, 2008 7:42 AM
In general an explicit function call should be preferred over an implicit cast so it's not wrong but a GetBuffer() call would be better style and of course you should otherwise be using _tcscpy_s :-D
"The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)
-
Umm... I'll have to agree. But you're a tough guy with that principle man. One must either know what CString is, or should not use it at all. :-D
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
Rajesh R Subramanian wrote:
you're a tough guy
:laugh: Not really. I certainly wouldn't apply that everywhere but CString is a bit if an exceptional case. CString abuse is so rife and so easy to fall into, and CString itself so potentially inefficient and error prone that I would say understand it or don't use it. I would not say the same for example for stl::vector or stl::map where misuse is less likely and understanding the source very much harder.
"The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)
-
That makes a copy. It is fine sometimes (and the other times it is wrong). :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeSir, just tell me where it will fail? I want to clear my confusion. Thanks:confused:
-
Sir, just tell me where it will fail? I want to clear my confusion. Thanks:confused:
Whenever you need to actually modify
CString
's internal buffer. It is not a common usage, I know, but it is perfectly legal.If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Whenever you need to actually modify
CString
's internal buffer. It is not a common usage, I know, but it is perfectly legal.If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeThank you sir :)
-
In general an explicit function call should be preferred over an implicit cast so it's not wrong but a GetBuffer() call would be better style and of course you should otherwise be using _tcscpy_s :-D
"The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)
Matthew Faithfull wrote:
it's not wrong but a GetBuffer() call would be better style
I don't agree. Implicit (or explicit) cast is not the same as
GetBuffer()
and you shouldn't use optionally one or the other:GetBuffer
returnsLPTSTR
, while the cast returnsLPCTSTR
: the addedC
have his significance. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Hi all.. I want to convert CString to _TCHAR* ////////////////////////////// _TCHAR *sEndDate; CString CurrDate; ///////////////////// I am trying this code.. sEndDate = (LPCSTR)CurrentDate; Its not working ... plz help me manju
Hi.. I am Mnaju.I have Completed my B.E Computers Science.Lokking for a job.I am interested in VC++ manju
-
Matthew Faithfull wrote:
it's not wrong but a GetBuffer() call would be better style
I don't agree. Implicit (or explicit) cast is not the same as
GetBuffer()
and you shouldn't use optionally one or the other:GetBuffer
returnsLPTSTR
, while the cast returnsLPCTSTR
: the addedC
have his significance. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeCPallini wrote:
the added C have his significance.
Indeed it has and although I was talking general C++ style I do think it applies in this case. The CString impilcit cast returns LPCSTR because it isn't safe for it to hand out a pointer to its internal buffer without locking it but it also isn't good C++ for it to 'silently' give you a const pointer to something that is inherently not const. It's a compromise brought on by a compromised design.
"The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)
-
CPallini wrote:
the added C have his significance.
Indeed it has and although I was talking general C++ style I do think it applies in this case. The CString impilcit cast returns LPCSTR because it isn't safe for it to hand out a pointer to its internal buffer without locking it but it also isn't good C++ for it to 'silently' give you a const pointer to something that is inherently not const. It's a compromise brought on by a compromised design.
"The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)
Well, let's try to get it from the
CString
's consumer point of view: (1) requesting, via (explicit) cast a pointer to a const buffer means: "OK, I need the buffer but I'll not change it". (2) requesting viaGetBuffer()
a pointer to the internal buffer means: "I need the buffer to make all the weirdest things I know to it". Clearly method (2) is a bit crude for a mere copy operation. :-DIf the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Well, let's try to get it from the
CString
's consumer point of view: (1) requesting, via (explicit) cast a pointer to a const buffer means: "OK, I need the buffer but I'll not change it". (2) requesting viaGetBuffer()
a pointer to the internal buffer means: "I need the buffer to make all the weirdest things I know to it". Clearly method (2) is a bit crude for a mere copy operation. :-DIf the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarkehi der, da getbuffr iz renamed in da latast sdk as
GetBufferIKnowWhatImDoing()
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP