Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi, I want to validate some da

Status
Not open for further replies.

ruimgp

IS-IT--Management
Feb 4, 2002
10
0
0
PT
Hi,

I want to validate some data and i'm using this

var1_2=00
var3_4=01

!strcmp(var1_2,"00") - validate that var1_2 is equal to 00

How do i do the oposite ?

strcmp(var3_4,"00") !=0 it seems to work wrong.

thanks
RP
 
Not sure what you're asking exactly, but strcmp() returns 0 when its string are equal and a non-zero value when they are not. So given two strings, s1 and s2:

if (strcmp(s1,s2)==0)
puts("equal");
else if (strcmp(s1,s2)>0)
puts("s1 greater than s2");
else
puts("s2 greater than s1");

Russ
bobbitts@hotmail.com
 
Hi,

By that, if true is 0 and false is 1 then
I'm doing this :
note that var3_4 = 01

if (strcmp(var3_4,"00") != 0 )
(...)

The != should validade both < and > but it didn't seem to work right.
What i want to do is as condition for situations where var3_4 is not equal to &quot;00&quot;.

Thanks.


 
Are your variables really character arrays?
They look like numerics to me - show us your definitions
:) Dickie Bird
db@dickiebird.freeserve.co.uk
 
I get the variables from a file like this:

file ->> 1|0|0|1234567890|001|256|

The position 4 of the file = 1234567890

char xpto[30];

strcpy (xpto, tokens[3]);

strncpy ( var3_4, xpto+2, 2);

Thats how i get value for var3_4 and var1_2

RP
 
Assuming that tokens[3] contains &quot;1234567890&quot;
following happens:
after strcpy(xpto, tokens[3]) xpto has &quot;1234567890&quot;
after strncpy(var3_4, xpto+2, 2) var3_4 has &quot;34..garbage..&quot; - do not forget that strncpy does not insert terminating 0.
What about var1_2 - you don't show how it is populated.

I suggest:
memset(var1_2, 0, sizeof(var1_2));
memset(var3_4, 0, sizeof(var3_4));
strncpy(var1_2, xpto, 2);
strncpy(var3_4, xpto + 2, 2);
if(strcmp(var1_2, &quot;00&quot;))
printf(&quot;not equal);
else
printf(&quot;equal&quot;);
etc...
OR
if(*var1_2 == '0' && *(var1_2 + 1) == '0')
printf(&quot;equal&quot;); etc...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top