strcmp for Last five characters
-
Hi, I want to compare last five character.Is there any direct syntax for that. Im geting secondary monitor size, to get that im using the follwing code
mi.cbSize = sizeof(mi);
GetMonitorInfo(hMonitor, &mi);mix.cbSize = sizeof(mix);
GetMonitorInfo(hMonitor, (MONITORINFO*)&mix);
CString display;
display.Format("%s" ,(LPSTR)mix.szDevice);if(strcmp(display,\\.\DISPLAY1)==0) //PROBLEM IS HERE
{
DualRect = mi.rcMonitor;
display.Format(" Res[%d,%d,%d,%d]",DualRect.left,DualRect.right,DualRect.top,DualRect.bottom);
AfxMessageBox(display);
}Here display get string as "\\.\DISPLAY1" but in stringcomparison,it doesnot get passed. Whats my mistake? Like this i will strcmp with DISPLAY2 string and then i will get that second monitor screen size.
Anu
-
Hi, I want to compare last five character.Is there any direct syntax for that. Im geting secondary monitor size, to get that im using the follwing code
mi.cbSize = sizeof(mi);
GetMonitorInfo(hMonitor, &mi);mix.cbSize = sizeof(mix);
GetMonitorInfo(hMonitor, (MONITORINFO*)&mix);
CString display;
display.Format("%s" ,(LPSTR)mix.szDevice);if(strcmp(display,\\.\DISPLAY1)==0) //PROBLEM IS HERE
{
DualRect = mi.rcMonitor;
display.Format(" Res[%d,%d,%d,%d]",DualRect.left,DualRect.right,DualRect.top,DualRect.bottom);
AfxMessageBox(display);
}Here display get string as "\\.\DISPLAY1" but in stringcomparison,it doesnot get passed. Whats my mistake? Like this i will strcmp with DISPLAY2 string and then i will get that second monitor screen size.
Anu
CString::ReverseFind[^] or if you're too much into C++, string::rfind or wstring::rfind can be used. I suggest you to use the CString version. You can find lot of string parsing functions in CString class. To find whether a monitor is a primary or secondary, you can use dwFlags parameter in the MONITORINFO structure [^]
-Sarath.
My blog - Sharing My Thoughts
Rate the answers and close your posts if it's answered
-
Hi, I want to compare last five character.Is there any direct syntax for that. Im geting secondary monitor size, to get that im using the follwing code
mi.cbSize = sizeof(mi);
GetMonitorInfo(hMonitor, &mi);mix.cbSize = sizeof(mix);
GetMonitorInfo(hMonitor, (MONITORINFO*)&mix);
CString display;
display.Format("%s" ,(LPSTR)mix.szDevice);if(strcmp(display,\\.\DISPLAY1)==0) //PROBLEM IS HERE
{
DualRect = mi.rcMonitor;
display.Format(" Res[%d,%d,%d,%d]",DualRect.left,DualRect.right,DualRect.top,DualRect.bottom);
AfxMessageBox(display);
}Here display get string as "\\.\DISPLAY1" but in stringcomparison,it doesnot get passed. Whats my mistake? Like this i will strcmp with DISPLAY2 string and then i will get that second monitor screen size.
Anu
since display is a CString, the = overload may be used. just try out this. if(display=="\\.\DISPLAY1") { ....... } and make sure, since you have \ as a character in a string, perhaps, you will have to use one more \ preceeding it.
-------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.
-
Hi, I want to compare last five character.Is there any direct syntax for that. Im geting secondary monitor size, to get that im using the follwing code
mi.cbSize = sizeof(mi);
GetMonitorInfo(hMonitor, &mi);mix.cbSize = sizeof(mix);
GetMonitorInfo(hMonitor, (MONITORINFO*)&mix);
CString display;
display.Format("%s" ,(LPSTR)mix.szDevice);if(strcmp(display,\\.\DISPLAY1)==0) //PROBLEM IS HERE
{
DualRect = mi.rcMonitor;
display.Format(" Res[%d,%d,%d,%d]",DualRect.left,DualRect.right,DualRect.top,DualRect.bottom);
AfxMessageBox(display);
}Here display get string as "\\.\DISPLAY1" but in stringcomparison,it doesnot get passed. Whats my mistake? Like this i will strcmp with DISPLAY2 string and then i will get that second monitor screen size.
Anu
Anu_Bala wrote:
if(strcmp(display,\\.\DISPLAY1)==0) //PROBLEM IS HERE
Well this will obviously not compile so I assume your copy and paste does not work. I am presuming that it should read:
if(strcmp(display, "\\\\.\\DISPLAY1")==0) //PROBLEM IS HERE
but since the '\' character in a string is used to escape the next character this is still wrong, the resultant string passed to
strcmp()
will be"\.dISPLAY1"
It should read:if(strcmp(display, "\\\\\\\\.\\\\DISPLAY1")==0)
It's time for a new signature.
-
Hi, I want to compare last five character.Is there any direct syntax for that. Im geting secondary monitor size, to get that im using the follwing code
mi.cbSize = sizeof(mi);
GetMonitorInfo(hMonitor, &mi);mix.cbSize = sizeof(mix);
GetMonitorInfo(hMonitor, (MONITORINFO*)&mix);
CString display;
display.Format("%s" ,(LPSTR)mix.szDevice);if(strcmp(display,\\.\DISPLAY1)==0) //PROBLEM IS HERE
{
DualRect = mi.rcMonitor;
display.Format(" Res[%d,%d,%d,%d]",DualRect.left,DualRect.right,DualRect.top,DualRect.bottom);
AfxMessageBox(display);
}Here display get string as "\\.\DISPLAY1" but in stringcomparison,it doesnot get passed. Whats my mistake? Like this i will strcmp with DISPLAY2 string and then i will get that second monitor screen size.
Anu
Hi Anu, Richard's hit the reason on the head as to why your code doesn't work. If you have this sort of problem again it always pays to see that the string you're doing the comparison with is correct - something as simple as:
::AfxMessageBox( "\\.\DISPLAY1" );
would have shown you something was wrong with the string you're comparing against. Cheers, Ash PS: Three other quick point: - You probably don't need to use CString::Format to copy the string from the structure, a constructor would have done the job. - While I don't use CString that often doesn't it have a comparison operator you can use instead of strcmp? - Instead of using CString why not use the string type that comes with the language? You only need CString when you're interfacing with MFC, the rest of the time std::string is a bit more flexible and with fewer conversion operators is less likely to blow up on you.
-
Hi, I want to compare last five character.Is there any direct syntax for that. Im geting secondary monitor size, to get that im using the follwing code
mi.cbSize = sizeof(mi);
GetMonitorInfo(hMonitor, &mi);mix.cbSize = sizeof(mix);
GetMonitorInfo(hMonitor, (MONITORINFO*)&mix);
CString display;
display.Format("%s" ,(LPSTR)mix.szDevice);if(strcmp(display,\\.\DISPLAY1)==0) //PROBLEM IS HERE
{
DualRect = mi.rcMonitor;
display.Format(" Res[%d,%d,%d,%d]",DualRect.left,DualRect.right,DualRect.top,DualRect.bottom);
AfxMessageBox(display);
}Here display get string as "\\.\DISPLAY1" but in stringcomparison,it doesnot get passed. Whats my mistake? Like this i will strcmp with DISPLAY2 string and then i will get that second monitor screen size.
Anu
-
If you wish to compare only specific string parts in C/C++ you could just use
strncmp
. :)Life is a stage and we are all actors!
Not quite. The
strncmp()
function lexicographically compares, at most, the first N characters in both strings. That is vastly different from which characters (e.g., middle 5, last 5) to compare."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
Not quite. The
strncmp()
function lexicographically compares, at most, the first N characters in both strings. That is vastly different from which characters (e.g., middle 5, last 5) to compare."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
Yes you are right but of course, but actually with a bit of simple logic you could compare any substrings using the same function, even the last n symbols for example:
char* a="Demo string";
char *b="Another string";if(strncmp((char*)(a+strlen(a)-6),(char*)(b+strlen(b)-6),6)==0)
{
printf("Last 6 symbols are equal.\n");
}
else
{
printf("Last 6 symbols aren't equal\n");
}It's not the best or even recommended practise, but It's possible.
Life is a stage and we are all actors!
-
Yes you are right but of course, but actually with a bit of simple logic you could compare any substrings using the same function, even the last n symbols for example:
char* a="Demo string";
char *b="Another string";if(strncmp((char*)(a+strlen(a)-6),(char*)(b+strlen(b)-6),6)==0)
{
printf("Last 6 symbols are equal.\n");
}
else
{
printf("Last 6 symbols aren't equal\n");
}It's not the best or even recommended practise, but It's possible.
Life is a stage and we are all actors!
Of course you can, since they are just addresses.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
Yes you are right but of course, but actually with a bit of simple logic you could compare any substrings using the same function, even the last n symbols for example:
char* a="Demo string";
char *b="Another string";if(strncmp((char*)(a+strlen(a)-6),(char*)(b+strlen(b)-6),6)==0)
{
printf("Last 6 symbols are equal.\n");
}
else
{
printf("Last 6 symbols aren't equal\n");
}It's not the best or even recommended practise, but It's possible.
Life is a stage and we are all actors!
simple and excellent logic...
Truth Can'nt be changed