Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-11-2003, 02:33 PM   #1
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
strcmp function does not seemed to work

strcmp function does 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");

single_char = getc(file_pointer);
word[i] = single_char;
printf("i = %d %c\n", i, word[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 ˇ
ˇ•ˇresult = 1
Old 06-11-2003, 04:24 PM   #2
Registered: Jan 2003
Location: Poland, Warsaw
Distribution: LFS, Gentoo
Posts: 591

Rep: Reputation: 32
char word[16];

string "FIREWALLGROUP=0" is 15 chars + null byte

while(!feof(file_pointer)) {... }

ends with i=16; index is out of range! (max is 14)
you need to control 'i' value inside the loop:
if(i++ >sizeof(word)) break;
Old 06-12-2003, 01:10 AM   #3
Registered: Sep 2002
Distribution: RH, FC, Ubuntu, Solaris, AIX
Posts: 114

Rep: Reputation: 15
All the string related functions like strcmp expect null terminated strings as parameters. The string that you were reading with 'file_pointer ' did not have a null character after '=0', therefore for strcmp function there was more data after '=0' too. In this scenario strcmp will read all the data from the start of the array 'word' until a null character is detected.

For your case you can also use 'strncmp()' function like this:

result = strncmp (word, "FIREWALLGROUP=0", 15);

Old 06-12-2003, 05:48 AM   #4
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30

Hi. I clicked on the button Affero. A new page comes up.
I rate the service on step 1
I enter the Affero Username: linh
Affero Password: my password

INVALID (Please note: Username and Password fields are case sensitive)

I cannot rate you or donate to the site.
How do I donate to this site ?
I like the service.
Old 06-12-2003, 06:09 AM   #5
Registered: Sep 2002
Distribution: RH, FC, Ubuntu, Solaris, AIX
Posts: 114

Rep: Reputation: 15
To rate/donate you have to be the member of afferno. Your have to register there first and after that u can rate someone. This username/password is saperate from the one you have at



Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How does the mail function work(PHP) Alexander.s Programming 1 05-15-2005 09:02 AM
c++ strings-strcmp function...... sachitha Programming 4 09-12-2004 07:28 AM
strcmp.. ways to make it work.. miguetoo Programming 1 03-08-2004 07:13 PM
bash - ftp doesn't work within a function richie-451 Programming 3 01-31-2002 01:21 PM
The search function should work MUCH better now. jeremy LQ Suggestions & Feedback 0 07-04-2001 12:30 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:26 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration