C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 11th, 2003, 02:34 PM
linh linh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 245 linh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 27 sec
Reputation Power: 10
strcmp function does not seemed to work

strcmp function does not seemed to work
result = strcmp (word, "FIREWALLGROUP=0"); this should be 0
but it is a 1. Nowhere in the code did I add a null (\0) character to
the array word.

word should be equal to FIREWALLGROUP=0

===============================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main()

{

FILE *file_pointer;
int i = 0;
char single_char;
char word[15];
int result = 99;

/*The statement below should yield FIREWALLGROUP=0 */
file_pointer = popen("cat /etc/yellowbox/network-config|grep FIREWALLGROUP | awk '{print $1}'", "r");

while(!feof(file_pointer))
{
single_char = getc(file_pointer);
word[i] = single_char;
printf("i = %d %c\n", i, word[i]);
i++;
}

printf("word = %s", word);
result = strcmp (word, "FIREWALLGROUP=0");
printf("result = %d\n", result);
====================================
The output result is shown below

root:~# ./test1
i = 0 F
i = 1 I
i = 2 R
i = 3 E
i = 4 W
i = 5 A
i = 6 L
i = 7 L
i = 8 G
i = 9 R
i = 10 O
i = 11 U
i = 12 P
i = 13 =
i = 14 0
i = 15

i = 16 ÿ
word = FIREWALLGROUP=0
ÿ¥ÿresult = 1

Reply With Quote
  #2  
Old June 11th, 2003, 02:52 PM
dmittner dmittner is offline
Dazed&Confused
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Tempe, AZ
Posts: 152 dmittner User rank is Sergeant (500 - 2000 Reputation Level)dmittner User rank is Sergeant (500 - 2000 Reputation Level)dmittner User rank is Sergeant (500 - 2000 Reputation Level)dmittner User rank is Sergeant (500 - 2000 Reputation Level)dmittner User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 22 h 22 m 18 sec
Reputation Power: 23
Send a message via ICQ to dmittner Send a message via AIM to dmittner
Not being too well versed in C, I might be off base here, but here goes...

strcmp returns the opposite of what you'd expect. If the passed variables match, 0 will be returned. 1 will be returned if they don't match.

I think that whoever designed the function just likes to make things confusing.

Reply With Quote
  #3  
Old June 11th, 2003, 03:04 PM
linh linh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 245 linh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 27 sec
Reputation Power: 10
have problem with strcmp or maybe it is my code

result = strcmp (word, "FIREWALLGROUP=0");

word = "FIREWALLGROUP=0"

I expect the variable result to be a 0 not a 1.
A valu of 0 means that they match.
I expect them to match, but they don 't.

Somebody help please.

Reply With Quote
  #4  
Old June 11th, 2003, 03:12 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,128 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 19 h 16 m 26 sec
Reputation Power: 1949
You left out the null-terminator, so word was greater than "FIREWALLGROUP=0", which is what the return value of 1 means. Your dump of word shows this; it looks like word[15] contains a '\n' and word[16] contains the EOF.

Try this:
Code:
single_char = getc(file_pointer);  /* read first character */
while(!feof(file_pointer) && single_char != '\n')
{
    word[ i ] = single_char;
    printf("i = %d %c\n", i, word[ i ]);
    i++;
    single_char = getc(file_pointer);
}
word[ i ] = '\0';  /* terminate that word! 
                         replace the '\n' with '\0' */

That should detect the new-line and terminate the string word in the proper place to get a valid compare.

Those garbage characters that came after the newline are a side-effect of your not having terminated the string and that first one, the 'ÿ', was printed out because of how your loop was written. You reached EOF when you tried to read past the newline, but you went ahead and added it to the string and output it. You see, when EOF is reached, getc() returns the value of EOF, which is a -1, which is a character value of 0xFF or 255, which is the character 'ÿ' (check Character Map if you have access to Windows).

Instead, you need to test for EOF before you treat the character returned by getc() as being valid. My rewrite should do that.

Last edited by dwise1_aol : June 11th, 2003 at 03:16 PM.

Reply With Quote
  #5  
Old June 11th, 2003, 03:13 PM
dmittner dmittner is offline
Dazed&Confused
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Tempe, AZ
Posts: 152 dmittner User rank is Sergeant (500 - 2000 Reputation Level)dmittner User rank is Sergeant (500 - 2000 Reputation Level)dmittner User rank is Sergeant (500 - 2000 Reputation Level)dmittner User rank is Sergeant (500 - 2000 Reputation Level)dmittner User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 22 h 22 m 18 sec
Reputation Power: 23
Send a message via ICQ to dmittner Send a message via AIM to dmittner
Re: have problem with strcmp or maybe it is my code

Quote:
Originally posted by linh
result = strcmp (word, "FIREWALLGROUP=0");

word = "FIREWALLGROUP=0"

I expect the variable result to be a 0 not a 1.
A valu of 0 means that they match.
I expect them to match, but they don 't.

Somebody help please.


Ah..sorry. Misunderstood you. Is the newline (\n) throwing off the match, maybe?

Again, sorry if I'm off base. Just kind of throwing ideas out.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > strcmp function does not seemed to work

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap