Hi all,
Having some trouble with a seemingly-simple task. Need to have a
basic CLI functionality as per:
prompt <- \n
prompt <- \n
promptquit <- should quit as per my snippet, but doesn't.
I've thrown a few random fflush()'s in, but no joy. I have never
really learnt the ANSI way ala fgets, fopen, etc.. so this is quite
new to me. Any advice on how I can actually make the program quit
when "quit" is entered would be appreciated :-)
Code:
#include <stdio.h>
int main()
{
int done=0;
int len=0;
char buf[MAXBUF];
printf("");
while(!done)
{
if(fgets(buf,MA XBUF,stdin) < 0)
{
perror("fgets") ;
exit(1);
}
len=strlen(buf) ;
buf[len]='\0';
fflush(stdin);
if(*buf == '\n')
printf("");
printf("debug: %s\n",buf);
fflush(stdin);
fflush(stdout);
if(!strncmp(buf ,"quit",len) )
done=1;
}
return 0;
}
thanks
kb
Having some trouble with a seemingly-simple task. Need to have a
basic CLI functionality as per:
prompt <- \n
prompt <- \n
promptquit <- should quit as per my snippet, but doesn't.
I've thrown a few random fflush()'s in, but no joy. I have never
really learnt the ANSI way ala fgets, fopen, etc.. so this is quite
new to me. Any advice on how I can actually make the program quit
when "quit" is entered would be appreciated :-)
Code:
#include <stdio.h>
int main()
{
int done=0;
int len=0;
char buf[MAXBUF];
printf("");
while(!done)
{
if(fgets(buf,MA XBUF,stdin) < 0)
{
perror("fgets") ;
exit(1);
}
len=strlen(buf) ;
buf[len]='\0';
fflush(stdin);
if(*buf == '\n')
printf("");
printf("debug: %s\n",buf);
fflush(stdin);
fflush(stdout);
if(!strncmp(buf ,"quit",len) )
done=1;
}
return 0;
}
thanks
kb
Comment