The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
strcmp function does not seemed to work
Discuss strcmp function does not seemed to work in the C Programming forum on Dev Shed. strcmp function does not seemed to work C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 11th, 2003, 02:34 PM
|
|
Contributing User
|
|
Join Date: Jun 2003
Posts: 245
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
|

June 11th, 2003, 02:52 PM
|
|
Dazed&Confused
|
|
Join Date: Jun 2002
Location: Tempe, AZ
|
|
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. 
|

June 11th, 2003, 03:04 PM
|
|
Contributing User
|
|
Join Date: Jun 2003
Posts: 245
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.
|

June 11th, 2003, 03:12 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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.
|

June 11th, 2003, 03:13 PM
|
|
Dazed&Confused
|
|
Join Date: Jun 2002
Location: Tempe, AZ
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|