[LCP]Char Comparisons?

Andrew Weaver Andrew.Weaver at tecnomen.fi
Thu Aug 29 18:36:06 UTC 2002


Yes, you need to do strcmp(buff, "load"). If it returns zero, then it's
true, else -1/+1

On Thursday, August 29, 2002 10:12 AM, James Mclean [SMTP:james at adam.com.au]
wrote:
> 
> All,
> 
> Im having some trouble getting a program to work as expected, i have a
> simple shell interface, and when a command is entered, it should execute
> a function.
> Now the problem i believe is the if statement, as the comparison is
> between a char and a regular text string. I know it is getting the
> string entered at the prompt in the buffer, as indicated by the printf
> just before the if statement, in the main loop.
> 
> Here is the code...
> 
> #include <stdio.h>
> 
> static void myexit(int sig) {
>     exit(0);
> }
> 
> void load(void) {
> 
>     char cpubuff[256];
>     FILE *fp;
>     
>     fp = fopen("/proc/loadavg","r");
>     fgets(cpubuff,256,fp);
>     fclose(fp);    	
>     printf("%s",&cpubuff);
> }
> 
> int usage(void) {
>     printf("You Git. Read the source!\n");
>     return 1;
> }
> 
> int main(int argc, char *argv[]) {
> 
>     char buf[256];
> 
>     for(;;) {
>         printf("> ");
>         scanf("%s",&buf);
> 
>         printf("%s\n",buf);
> 	
> 	if(buf=="load") {
> 	    load();
> 	} else {
> 	    usage();
> 	}
>     }
> 
>     return 0;
> }
> 
> TIA, any help is great :)
> 
> 
> Regards,
> 
> James Mclean
> 
> "Increased knowledge will help you now.  
>  Have a mate's phone bugged."
> 		-- /usr/games/fortune
> 
> _______________________________________________
> This is the Linux C Programming List
> :  http://lists.linux.org.au/listinfo/linuxcprogramming List




More information about the linuxCprogramming mailing list