|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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. ![]() |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
Re: have problem with strcmp or maybe it is my code
Quote:
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > strcmp function does not seemed to work |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|