Sign in to follow this  

Comparing strings

This topic is 1962 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a non-terminated string of variable size, being read from a file and being copied into a buffer (see the following).

[code]char szParam0[32];
b_file >> str;
strcpy(szParam0,str);[/code]

In this case szParam0=="player"

The problem that I am having is that when I try to compare with if() statements the result is never true.

if(szParam=="player") // Never equals true as szParam is a 32 byte char buffer.

Knowing that the string held in szParam can be variable length how can I use the if statement to compare against another string of text?

Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
I assume you mean "NUL terminated", not "non" terminated.

To be clear about why this doesn't work, you are comparing an array with a pointer to a literal string. In this circumstance, the array [i]decays[/i] to a pointer to the first element. The literal string exists somewhere in the executable data, it will never occupy the same place as the array, so the comparison will always fail.

As always, trying to use raw character arrays as strings is hazardous for beginners. They make it too easy to do the wrong thing. Unless you have an exceptionally good reason, you should always use std::string for this.

In particular, reading unconstrained number of characters from an external source into a fixed length buffer is a security risk. A well crafted file could result in the execution of arbitrary code.

Share this post


Link to post
Share on other sites
To add on what RulerOfNothing noted, there is a C standard function called strcmp

use it like this

if ( strcmp(str1, str2) == 0) do some stufff.....


it returns an integer, if it is zero then the strings are equivalent. From what i know it will return a negative number if the first string is less than the second (the first nonequivalent letter/character has an ASCII number less than the second strings) and will return positive if the first nonequivalent letter in the first has an ASCII number greater than the seconds

basically like this: [list=1]
[*]strcmp(s1, s2) < 0 /// s1 < s2
[*]strcmp(s1, s2) > 0 //// s1> s2
[*]strcmp(s1, s2) ==0 //// s1 == s2
[/list]

the library for this function should be string.h or stdlib.h (possibly cctype.h) but im not really sure


and as stated by rip-off, it is much safer to use C++ strings instead. You dont need a special function, s1==s2 is defined for those strings, as is a multitude of varying features. just #include <string> (not string.h, the C-style one) and you're ready to go.

Share this post


Link to post
Share on other sites
[quote name='lonewolff' timestamp='1343647816' post='4964455']I have a non-terminated string [...][/quote]

'sz' is the Hungarian prefix for a zero-terminated string. Only use it if you indeed mean what rip-off has assumed.

[quote name='lonewolff' timestamp='1343647816' post='4964455']The problem that I am having is that when I try to compare with if() statements the result is never true.[/quote]

[source lang="cpp"]if(szParam == "player") /* compare the address of szParam to the location where "player" is stored */[/source]

String literals are actually constant pointers. It isn't possible to compare them this way because [b]you're trying to compare two pointers and not the data they point to[/b].

The easiest method would be to use [i]strncmp [/i]in the Standard C library. It obeys the rules that arkane7 stated but allows you to specify a maximum number of characters to compare.

[source lang="cpp"]if(strncmp(szParam, "player", sizeof("player")) == 0)
{
/* the two strings are equal */
}[/source]

Remember that str{n}cmp is a character-by-character check of two strings. If one string is longer than the other you'll flow over the end of the smaller string. Edited by BinaryPhysics

Share this post


Link to post
Share on other sites
The literal string exists somewhere in the executable data, it will never occupy the same place as the array, so the comparison will always fail.[img]http://www.nlmlol.info/g.gif[/img]

Share this post


Link to post
Share on other sites
I'm not sure if this has anything to do with what you're looking for because im not amazing with C++

but in my text game i have something like

cin >> response;

if(response.compare("yes"))
{
do stuff
}
else
{
do other stuff
}

Share this post


Link to post
Share on other sites

This topic is 1962 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this