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!

strcmp problem 1

Status
Not open for further replies.

marsd

IS-IT--Management
Apr 25, 2001
2,218
0
0
US
Most of the time I keep away from C with manipulating/testing strings, but this time I do
need to do it.

I'm using strcasecmp::
char *buf[50];

fgets(buf,50,stdin);
if (strcasecmp(buf,"quit") == 0) {
do_cleanup(fd1,fd2,arrayname)
} else {
etc..

Line no 1: quit
Array storage quit

0

Line no 2: quit
Array storage quit

0

Line no 3: quit
Array storage quit

0

Line no 4: QUIT
Array storage QUIT

0

According to the man pages this should not happen.
Any ideas?
 
First of all, you haven't reserved any memory to buf.

Since you seem to want to use a static array of characters you should skip the *.

Also, fgets() doesn't stip the newline character. Hence buf will contain "quit\n" instead of "quit".

strncasecmp() lets you specify how many characters that will be compared.

The following code might make things clearer..

#include <stdio.h>

int main()
{
char buf[50];
int i;

do {
fgets(buf,50,stdin);
printf(&quot;buf contains '%s'\n&quot;, buf);
} while (strncasecmp(buf,&quot;quit&quot;, 4) != 0);


return 0;
}
 
No I misposted, my fault...
char buf[50];
Not an array of 50 char *ptr's without memory,
I'm a little better than that ;)


Thanks for the fgets() info, I knew it had to be something simple, so the fix is:
(strcasecmp(buf,&quot;quit&quot;,strlen(buf) - 1) == 0)

Great, something easy for a change.
Thanks.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top