Can anyone tell me if this is correct
........................
if
( (strcmp( returnVal, "<name> %s </name>" )!=0 ) ) {................ where returnVal a char*.If not is there any way to correct it
Can anyone tell me if this is correct
........................
if
( (strcmp( returnVal, "<name> %s </name>" )!=0 ) ) {................ where returnVal a char*.If not is there any way to correct it
Character* in string comparison....
Phatsuo
Oshah said:
"This line looks fine to me, as long as returnVal is declared as:
char *returnVal;
or
const char *returnVal = "foo";
don't forget, strcmp returns 0 if the strings are equal, and something other than 0 if they're not (this will reverse the boolean tests)."
Yes,returnVal is a char * as I said.I ain't disagree with that.Look at this part of my code:
returnVal = gets_s( line );
if( strcmp( returnVal, "<name> %s </name>" )!=0 ) {printf( " Error in line...:%s\n", returnVal );
returnVal = gets_s( line );
} else {
printf("ok");
My question is:If I write <name> %s </name> where %s a string will it print ok
Sorry for not being specific before!
JasonBSteele
TPelkmans
venky_m
To be honest though, it would be better to split that line up to test. Since you want to know if you have a <name> (string) </name> in your line then you should parse the line first to get the <name> and </name> matched then everything else will be the value.
But there is one problem with this I think. What happens, in the very likely even that someone writes.
<name>
Me
</name>
With what you are trying to do, this may result in an error with the way you are writing it, unless you have it ignoring newline characters.
KG
From what I can tell, you want to know if you could use strcmp to perform some kind of wildcard matching: ie. you want input of "<name>asdf</name>" to branch to printf("ok").
strcmp will only return true if you literally typed "<name>%s</name>". It won't work if you need strcmp to understand %s as a wilcard (like the "<name>asdf</name>" search above). If you need this, you'll need to use something more advanced.
TonyMan - MSFT
It is important to become familiar with the standard libraries (e.g., the C Language Standard Library with header files string.h, stdio.h, etc.). There's comprehensive information in all good C/C++ books and also online at MSDN. (I don't have VC++ EE installed at the moment, but library references may be part of the MSDN resources and Intellisense included in VC++ EE.)
The kind of pattern matching you are attempting is usually done by using regular-expression functions or, these days, XML parsing libraries. There's an MSXML library that might work for you, since you are using markup. (Go on MSDN and search for it.)
There is no standard support for regular expressions in C/C++. To find information about other libraries that have been developed, search the web for "regexp." The Boost Regex library, which may work its way into future C++ standards, has support for pattern matching over strings. There are also open-source regexp implementations that work in C and that you can include into a project and call from your code. (Search the web.)
My only concern is that learning how to work with these advanced libraries will require proficiency and comfort with the basics and also with the way additional libraries are integrated into your VC++ projects. For boost, there's also heavy reliance on advanced C++ concepts. It is difficult to tell, here, whether you are prepared for that. Many of these materials depend on considerable tacit knowledge and experience to be used successfully. I don't know of a decent beginner road map, if that is what you need. I would love to be mistaken about that.
Finally, if you are willing to develop for the CLR, there are other resources. I would start with Visual C# to do that though. It is too confusing to sort out the different ways VC++ programs can be hosted. It's another tacit knowledge and experience problem for which I have been unable to find a good beginner roadmap.
stefansve
This line looks fine to me, as long as returnVal is declared as:
char *returnVal;
or
const char *returnVal = "foo";
don't forget, strcmp returns 0 if the strings are equal, and something other than 0 if they're not (this will reverse the boolean tests).
jrcran
I'm not sure if low level splitting/parsing the string is the best option. It won't scale as well as orcmid's regex solution when they parsing needs to go beyond <name> </name> pairs.
One other problem to think about, how do we proceed when the user types one of these strings:
<name><name></name>
<name></name></name>
<NAME>asdf</name></NAME>
etc.
Mahavir
Well, I've replied under the assumption that this person is a novice programmer learning how to do these things. If I wasn't assuming that then I'd suggest using an XML library because they are much easier to use.