|
Hi CPallini,
Actually I am also confused when I tried that,
"Hello 'World'" is the same as "Hello \'World\'". I have also tried your sample. Now I am more confused...
1. What is the rule when we should use \' or single ' ?
2. When there is no differences between \' and '? (like my Hello 'World' sample)
regards,
George
|
|
|
|
|
Hi.
You use single ' inside a string delimited by " characters
i.e.
char* p2 = "Hello 'World'";
You use the \' when iusing it as a single character that need to be quoted within ' characters
i.e.
char p1 = '\'';
Habetis bona deum
|
|
|
|
|
Good reply, thanks DoomedOne!
regards,
George
|
|
|
|
|
as C Pallini stated, you need to use \' only when the compiler can get confused.
technically, \' is the same ascii code as the character ' .
just same as \" is the same ascii code as the character " .
so, in brief, ben you use ' in a string (so, rounded with " s), you don't need to use the escapment char ( \ ).
when you use " as a single char, no need either to use \ .
some examples:
"Hello \'World\'" is the same as "Hello 'World'" .
"Hello "World"" is forbidden because the second " char is understood as the end of the string.
here, you MUST write: "Hello \"World\""
'"' is the ascii code of the " character.
''' is forbidden. here again, the second ' char is understood as the end of the character specification.
You MUST write: '\'' .
get it now ?
subsidiary question: please, for god' sake, in what school level are you, and how old are you ? i'm not judging you, i'm questionning myself.
|
|
|
|
|
Cool toxcct!
I must save your reply to somewhere.
regards,
George
|
|
|
|
|
hi,
I am using the function ConvertResourceStringIdToLptstr() to assign the value of the TV_INSERTSTRUCT's pszText for the creation/initialization of a TreeCtrl. (or) to convert the 'resource string' into a pointer to a character string.
<br />
LPTSTR COperDialog::ConvertResourceStringIdToLptstr(UINT nResString)<br />
{<br />
CString strLoad;<br />
strLoad.LoadString(nResString);<br />
LPTSTR lpsz = new TCHAR[strLoad.GetLength()+1];<br />
CHECK_FOR_NULL(lpsz)<br />
_tcscpy(lpsz,strLoad);<br />
return lpsz;<br />
}<br />
And the fuction is used in:
<br />
void COperDialog::AddProjectRootItem()<br />
{<br />
TV_INSERTSTRUCT curTreeItem;<br />
<br />
curTreeItem.hParent =TVI_ROOT ;<br />
curTreeItem.hInsertAfter = TVI_ROOT ;<br />
curTreeItem.item.iImage = 0;<br />
curTreeItem.item.iSelectedImage = 0;<br />
<br />
curTreeItem.item.pszText = ConvertResourceStringIdToLptstr(IDS_PROJECT);<br />
curTreeItem.item.cchTextMax=15;<br />
<br />
curTreeItem.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;<br />
<br />
htrProjectRoot = m_Edit->InsertItem(&curTreeItem); <br />
<br />
}<br />
In this situation i am getting memory leaks in ConvertResourceStringIdToLptstr().
So to avoid that where should i insert the delete[]? If i use it inside the function - ConvertResourceStringIdToLptstr() i am not able to get the string displayed!
Kindly give sugesstions!!
Priya Sundar
|
|
|
|
|
You can use the pointer as member or global variable, fill it, use it and when you dont need it anymore... use delete. That way you will have the control about the pointer along the whole class / project.
LPSTR pMyLpstr;
.
pMyLpstr = NULL;
.
LPTSTR COperDialog::ConvertResourceStringIdToLptstr(UINT nResString)
{
CString strLoad;
strLoad.LoadString(nResString);
pMyLpstr = new TCHAR[strLoad.GetLength()+1];
CHECK_FOR_NULL(pMyLpstr)
_tcscpy(pMyLpstr,strLoad);
return pMyLpstr;
}
.
void COperDialog::AddProjectRootItem()
{
TV_INSERTSTRUCT curTreeItem;
curTreeItem.hParent =TVI_ROOT ;
curTreeItem.hInsertAfter = TVI_ROOT ;
curTreeItem.item.iImage = 0;
curTreeItem.item.iSelectedImage = 0;
curTreeItem.item.pszText = ConvertResourceStringIdToLptstr(IDS_PROJECT);
delete [] pMyLpstr;
pMyLpstr = NULL;
curTreeItem.item.cchTextMax=15;
curTreeItem.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;
htrProjectRoot = m_Edit->InsertItem(&curTreeItem);
}
.
delete [] pMyLpstr;
pMyLpstr = NULL;
Hope it helps
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
|
|
|
|
|
That way you have to be very careful.
I think it is an hazardous design.
Probably it would be better if both memory allocation and deallocation are delegated to the caller (e.g. the standard behaviour of Windows API).
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.
|
|
|
|
|
Hi,
CPallini wrote: Probably it would be better if both memory allocation and deallocation are delegated to the caller (e.g. the standard behaviour of Windows API).
I didnot understand what you are referring to??
Priya Sundar
|
|
|
|
|
Was a note on Nelek suggestion.
For instance GetWindowText WIN32 API work that way, the caller has to
(1) Allocate a buffer
const int SIZE = 256;
TCHAR * buffer = new TCHAR[SIZE];
(2) pass it (together as is size) as function argument
if ( buffer)
{
GetWindowText(hWnd, buffer, SIZE);
}
(3) release the buffer
if (buffer) delete buffer;
buffer = NULL;
BTW all the above stuff can be done (and the life will be easier...) using the stack instead of the heap.
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.
|
|
|
|
|
Can you explain a bit what are you referring to? I don't understand you well and I want to learn more.
That this is not the best option... I agree (I know I am just a low/middle skilled programmer that needs to learn more), but... what do you mean with the standard behaviour of windows api? can you put an example?
Maybe something like this?
LPTSTR COperDialog::ConvertResourceStringIdToLptstr(UINT nResString, LPSTR pStr)
{
CString strLoad;
strLoad.LoadString(nResString);
pStr = new TCHAR[strLoad.GetLength()+1];
CHECK_FOR_NULL(pStr)
_tcscpy(pStr,strLoad);
return pStr;
}
void COperDialog::AddProjectRootItem()
{
TV_INSERTSTRUCT curTreeItem;
curTreeItem.hParent =TVI_ROOT;
curTreeItem.hInsertAfter = TVI_ROOT;
curTreeItem.item.iImage = 0;
curTreeItem.item.iSelectedImage = 0;
;
LPSTR pMyLpstr = NULL;
curTreeItem.item.pszText = ConvertResourceStringIdToLptstr(IDS_PROJECT, pMyLpstr);
;
delete [] pMyLpstr; pMyLpstr = NULL;
curTreeItem.item.cchTextMax=15;
curTreeItem.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;
htrProjectRoot = m_Edit->InsertItem(&curTreeItem);
}
Is this option better than the other one?
-- modified at 8:11 Friday 23rd November, 2007
-- modified at 8:12 Friday 23rd November, 2007
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
|
|
|
|
|
Nelek wrote: Maybe something like this?
No.
Nelek wrote: LPTSTR COperDialog::ConvertResourceStringIdToLptstr(UINT nResString, LPSTR pStr) //parameter added
The parameter added is irrelevant.
I meant the standard behaviour of WIN32 API, for instance:
int GetWindowText( HWND hWnd, LPTSTR lpString, int nMaxCount );
The above function doesn't allocate the buffer, it relies on the one allocated by the caller.
Please note that your suggestion is a perfectly viable solution I only noted that it must be handled with care.
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.
|
|
|
|
|
Yes, it can be viable, but I prefer to know the best ways to do things.
CPallini wrote: int GetWindowText( HWND hWnd, LPTSTR lpString, int nMaxCount );
The above function doesn't allocate the buffer, it relies on the one allocated by the caller.
So before calling the function you have to create the LPTSTR lpString to give him as a parameter, and after the use delete it. And what is the difference with my second option? That the new is inside the function that is being called? For this schema... how can be dealed? To use the new, it must be known the length of the loadString that is calculated inside the called function. (I know, resctructurating and not using the function is a solution)
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
|
|
|
|
|
Nelek wrote: And what is the difference with my second option? That the new is inside the function that is being called?
Yes, the caller has total responsibility on buffer allocation/deletion. This, IMHO is a quite acceptable design choice.
On the other hand, I insist on saying that yours is a viable solution.
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.
|
|
|
|
|
Thanks for answering,
CPallini wrote: On the other hand, I insist on saying that yours is a viable solution.
I know. But I want to learn, and be better. I like to programm against idiots (although I know is very complicated) and my programms usually are working in 99% of time and situations. But that doesn't mean that are "designly correct". I actually studied electronic engineering not informatic. But that doesn't mean that I may not be able to be a good programer, if I learn with the bests
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
|
|
|
|
|
Nelek wrote: I like to programm against idiots
Oh, it is a lost battle.
Nelek wrote: I actually studied electronic engineering not informatic. But that doesn't mean that I may not be able to be a good programer, if I learn with the bests.
I think the same about me (forced point: I have a physics background ).
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.
|
|
|
|
|
Very glad to know people!
I am also from an ECE(Electronic & Comm Eng). I guess i can get encouraged to do better in programming by you all!!
Thankyou!
Priya Sundar
|
|
|
|
|
Thanks a lot!!
It did solve my problem for the time being.
As the other person has commented, as this is sort of hazardous, will try if there is any other way of doing it.
Again thanks a lot!
Sometimes, too much into coding... then a simple common sense logic also seems to be complex!
Have a nice evening!
Priya Sundar
|
|
|
|
|
Discard your endless-name-function and simply do:
...
CString strLoad;
strLoad.LoadString(nResString);
curTreeItem.item.pszText =(LPTSTR)(LPCTSTR) strLoad;
...
You can safely do that since you're inserting an item, i.e. you know that your buffer will only read.
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.
|
|
|
|
|
I need to apply this for some 50 strings.. so it will be bulky code if i write these statements for every treectrl item. So i need it as a function which will be just a single line of code!
Thankyou for your suggestion!
Priya Sundar
|
|
|
|
|
Priya_Sundar wrote: I need to apply this for some 50 strings
Good to know. In such a case you may prefer to allocate a single buffer instead of doing all that allocation/deallocation stuff.
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.
|
|
|
|
|
Could you help me more with respect to the code i have posted? i.e how you want me to allocate single buffer??
Priya Sundar
|
|
|
|
|
At the very beginnning of the process you may allocate a buffer (wide enaough to contain the biggest string you're dealing with), then use it for the retrieval of every string, maybe using the raw WIN32 API
int LoadString( HINSTANCE hInstance, UINT uID, LPTSTR lpBuffer, int nBufferMax);
finally you deallocate the buffer when the last string has been retrieved.
You can also rely on the stack for such purpose:
{
const int SIZE=1000;
TCHAR buffer[SIZE];
LoadString( hInstance, uID1, buffer, SIZE);
...
LoadString( hInstance, uID2, buffer, SIZE);
...
LoadString( hInstance, uIDN, buffer, SIZE);
...
}
Note: I omitted (leaving it to the reader) the error checking code.
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.
|
|
|
|
|
Hi,
Thank you very much.
I am able to get the strings displayed properly in the RELEASE VERSION. But in debug version some special characters are getting displayed as some letters as the part of the required string to be displayed!
Do you have any suggestions so as to explain why it is happening like that??
Also at the initial stage of my program when the string resource has not been yet initialized by the program, there are some global variables where i am assinging some strings.
Do you have any idea regarding how to load string values for such variables from the string table which at that point have not been initialized(eg. registry var).
Appreciate your help! Thankyou once again!!
Priya Sundar
|
|
|
|
|
hi,
i am using VC++ 6 editor and i am handling a project that was done by another team of developers and i am having a problem in debugging the static library in the project workspace. when i am trying to put a breakpoint in the static library source file the compilor will disable the breakpoint and move the cursour to the winmain. The following message box window is comming while i try on F5.
---------------------------
Microsoft Visual C++
---------------------------
One or more breakpoints cannot be set and have been disabled. Execution will stop at the beginning of the program.
---------------------------
OK
---------------------------
My work space contained of two exe projects and 3 static librararies and a Dll project all the exe and dll projects allow me to put the break point in the source editor and allow me to debug. But the static library project editor is not allowing to do the debug. What is the Problem. Is i am missing anything. Need your help urgently.
Thanks
Shine joseph
|
|
|
|