|
CString already provides such an operator...
CString str = "Hello you";
char* pszTxt = (LPTSTR)str;
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
Thanks for your answering, but when I try doing the following code
LPTSTR CMainDlg::XuLyChuoi()
{
char *tmp_sz = new char[1000];
::GetWindowText(GetDlgItem(IDC_EDIT1),tmp_sz,1000);
CString s = tmp_sz;
int index = 1;
while ((index=s.Find("x", index)) != -1)
{
CString s1 = s[index-1];
s1+= s[index];
CString s2 = s[index-1];
s2+= "*";
s2+= s[index];
s.Replace(s1, s2);
}
tmp_sz = (LPTSTR)s;
return tmp_sz;
}
I have the error:
error C2440: 'type cast' : cannot convert from 'class WTL::CString' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.
- 1 error(s), 0 warning(s)
Can you explain?Thank you.
|
|
|
|
|
put aroud your literal string some _T() because you seem to use UNICODE...
and also declare tmp_sz as a TCHAR* , not a char*
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
thanks again, but it still give me that error.
|
|
|
|
|
LPTSTR CMainDlg::XuLyChuoi() {
TCHAR* tmp_sz = <font color=blue>new</font> TCHAR[1000];
::GetWindowText(GetDlgItem(IDC_EDIT1), tmp_sz, 1000*<font color=blue>sizeof</font>(TCHAR));
CString s = tmp_sz;
<font color=blue>int</font> index = 1;
<font color=blue>while</font> ((index = s.Find(_T(<font color=gray>"x"</font>), index)) != -1) {
CString s1 = s[index-1];
s1 += s[index];
CString s2 = s[index-1];
s2 += _T(<font color=gray>"*"</font>);
s2 += s[index];
s.Replace(s1, s2);
}
::_tcscpy(tmp_sz, (LPTSTR)s); <font color=green>
<font color=blue>return</font> tmp_sz;
}
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
Not relating to the use of strings or conversion etc, but it looks to me like you have a chance of memory leakage here.
You allocate memory for the string on the heap, then copy this into a CString. Later on you change the pointer to point to the LPTSTR of the CString (I think it should maybe be LPCTSTR, as casting a CString will probably only be safe if cast to a const - which may explain the error you're getting).
This means that you lose the memory you allocated at the start of the function, as nothing's pointing to it any more. Also, you're returning a pointer to something which is about to be deleted (the CString will be deleted when you exit the function).
I think what you actually should be doing (tho' feel free to correct me) is to copy the contents of the CString into the address pointed to by tmp_sz, and then return the tmp_sz pointer. Just remember that you need to later on delete the pointer that you're returning.
So your last bit of code would do something like:
strcpy(tmp_sz, (LPCTSTR)s, s.GetLength());
return tmp_sz;
After making sure that the length of CString s is not > 1000 (including the NULL terminator).
Does that make sense? I'm speed-typing coz it's lunchtime and I'm hungry.
|
|
|
|
|
Doing the following would work.
strncpy (tmp_sz, s, 1000);
tmp_sz [999] = 0; // strncpy doesn't place a null on a full buffer
return tmp_sz;
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
cauvang wrote:
convert a string from CString(MFC) to LPTSTR
Are you certain that your CString is an MFC CString?
I ask this because in the more detailed answer your gave toxcct here[^] looks like your using a CString defined in a namespace called 'WTL' which could indicate that it's all about the WTL version of CString you are using. I don't know anything about WTL so I won't be able to guide you there.
Check this out. If you're not using the MFC version our answers won't make sense.
Perhaps you even should post your question in the WTL forum.
--
Roger
|
|
|
|
|
I'm really sorry! I want to convert a string from CString(MFC) to LPCTSTR but the example is really stupid( it was written in WTL !!!).
Now I wonder how to use a WTL class ( written in WTL,used WTL CString ) in a MFC application. The class is so important and i need it in my MFC project.However,it was written in WTL using WTL CString.Help me!!
|
|
|
|
|
i am saving some data to file(CStdio class) in the following format.
Address1-Description1
Address2-Description2
Address3-Description3
When ever i want to retrive data i search for the address and get the description.
Now when i change the description, it is required that i replace the old description with the new one.
One way is to re create the whole file again the only change of new description.
This is really inefficient.
Can you tell me some nice logic to do this
Thanks a lot
|
|
|
|
|
Hello,
I don't think that it is possible to just 'insert' data in a file. When you write somewhere, data will be overwritten.
You have 2 options. You can insert buffers in the file (after an address, description, insert spaces). This way, you can overwrite the description and you have the option to grow until the specified buffer size.
The second option you have is to copy all the data after the description that you want to change and write it again after you update the description.
If you change the description frequently, I advice option 1. This is much faster. If the changes occurr less ofter and you have lot's of addresses, I suggest the second option. This saves you a lot of space, but it takes more time when you update your file.
Hope this helps.
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
thanks for your suggestions, still i have some things to clarify.
I have just 256 addresses so i will go with rewriting the file. But to do this i need to save the rest of the file temporarily, what do you think the data structure i have to use to do this.
Thanks again.
|
|
|
|
|
Hello,
First, I assume that you are using MFC's CStdioFile class since I don't know of a CStdio class.
You can do the following:
CList<CString> StringList;
CString sLine = "";
while( YourFileObject.ReadString(sLine) )
{
StringList.AddTail(sLine);
}
while( !StringList.IsEmpty() )
{
YourFileObject.WriteString(StringList.RemoveHead());
}
Hope this helps
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
One idea would be to use a slightly different format for your file...like an .ini file.
[Addresses]<br />
Address1=Description1<br />
Address2=Description2<br />
Address3=Description3
You could then use GetPrivateProfileString and WritePrivateProfileString to read/change the values.
Hope that helps.
Karl - WK5M
PP-ASEL-IA (N43CS)
<kmedcalf@ev1.net>
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
|
|
|
|
|
Hi
I have simple windows DLL.
In my DLL i have a function named as "MyFunc" which contains three parameters from which two are "in" parameter and the third one "out" parameter. I am sure I m getting the value in my third parameter. But when I try to call it using Visual Basic programm, I am not getting the value ? What seems the problem....
I am writing whole code for your reference here..
VC++ DLL Function code :
========================
void __stdcall MyFunc(char *sMyString, long lType, char **sResult)
{
char sTemp[255] = "Mahesh";
*sResult = new char[512];
strcpy(*sResult,"");
strcat(*sResult,sTemp);
........
........
........
strcat(*sResult, "; ");
return;
}
VB Programm Code to test....
===========================
Dim sReference As String
Private Declare Sub MyFunc Lib "C:\....\MyDLL.dll" (ByVal sMyString As String, ByVal PrdID As Long, ByRef sOut As Variant)
Private Sub Command1_Click()
Dim sPrefix As String
Dim sResult As String
sPrefix = "CST"
sReference = Space(27)
Call AiGetLicense(sPrefix, 3, sOut)
MsgBox (sOut)
End Sub
==============================================
Here I get sOut as balnk
But when I debug my DLL i get value in *sResult as "Mahesh;"
What seeems to be the problem ?
Any idea plz ?
Thankx a lot
Amarelia Maehsh
Gujarat
India
|
|
|
|
|
Amarelia wrote:
void __stdcall MyFunc(char *sMyString, long lType, char **sResult)
Amarelia wrote:
Private Declare Sub MyFunc Lib "C:\....\MyDLL.dll" (ByVal sMyString As String, ByVal PrdID As Long, ByRef sOut As Variant)
shouldnt the 3rd param be of varient type in VC++?
or sOut of type string?
-prakash
|
|
|
|
|
hallo...
can someone help me on this give me an full example of source code...?????
or used a link listed queue?????/
Nadz
|
|
|
|
|
Won't STL's priority_queue do for you?
There's a nice sample in MSDN of it too.
Hope this helps
--
Roger
|
|
|
|
|
I agree, no point on working hours to build a less powerful, untested version of something that is part of the compiler / language.
John
-- modified at 8:15 Wednesday 12th October, 2005
|
|
|
|
|
John M. Drescher wrote:
no point...
I can think of one very important point. If a person wants to truly understand how a particular data structure works, you must create one from scratch. Using a built-in one does not accomplish this goal.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
Hello all,
can anyone tell me how I can add new build configuration??
In new project are two conf. Debug and Release.
I would like add Debug Unicode, Release Unicode ....
But there in no choise to do this ....
please help me
Thanx xSoptik
|
|
|
|
|
What about the menu Build/Configurations/Add ?
(That's what it looks like in VC++ 6.0 anyway).
|
|
|
|
|
Thanx
In VC7 there is a little different situation.
That choose is hidden in popup menu.
|
|
|
|
|
when i compile a program, an error occur,as below:
"C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\atlconv.h(996) : error C2084: fucntion“LPDEVMODEA AtlDevModeW2A(LPDEVMODEA,const DEVMODEW *)”already Has a
body......."
is there anybody know how to solve this?
MS Visual Studio .net 2003
Windows Xp Professional SP2
Additional: Everything is ok under Windows2000
hello everybody!
|
|
|
|
|
Hi.
In visual c++ i want to create one class in order show algorithm sort by graphic.
Example sort: 1 4 6 5 8
I want to exchange 6 and 5 by graphic.
class CricleObject{
//attribute
int x, y, r;
int value;
//method
CricleObject();//constructor
CricleObject();
void moveLeft();
void moveRight();
};
In algorithm code:
//CricleObject array[10];
if (array[1].Value > array[2].Value){
array[1].moveRight();
array[2].moveLeft();
}
...
They are show by graphic.
Thanks.
-- modified at 22:11 Tuesday 11th October, 2005
|
|
|
|