[LCP]Char Comparisons?

Rokicki, Andrew ARokicki at US.TIAuto.com
Fri Aug 30 22:31:06 UTC 2002


if(buf=="load")  I don't think you want to do this.

look up:
man string
or
man 3 strings

You need to do string opperation here.
what you are doing you are comparing pointer to "load" which will be false.
you could do 
if (buff[0]=='l') 
this with comapare first character.



On Friday, August 30, 2002 1:13 AM, devraj  sanyal
[SMTP:devraj_sanyal at rediffmail.com] wrote:
> Its better to learn some C programming then come to
> unix internals.
> Try out kerningham and ritchie.
> 
> devraj
> 
> 
> 
> On Thu, 29 Aug 2002 James Mclean 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
> 
> 
> _______________________________________________
> This is the Linux C Programming List
> :  http://lists.linux.org.au/listinfo/linuxcprogramming List




More information about the linuxCprogramming mailing list