This question already has an answer here:
I have follow situation I have an program make an set of operations on a file continuously and I want, when a specific key is pressed, to stop and do another set of operations.
For this I tryed use scanf
of an character with
fcntl(0, F_SETFL, O_NONBLOCK);
and
while(feof(stdin))
but it doesn't work as expected.
I have searched and in some places someone says to use select
but I can't find how to use it.
Anyone can advice me somehow?
my main function to provide more information:
int main(int argc, char *argv[])
{
if(argc>1){
char* password;
char* password2;
printf("Password? ");
password = get_password();
printf("Repita a password? ");
password2 = get_password();
if(strcmp(password,password2)!=0){
printf("Passwords diferentes!\n");
exit(1);
}
FILE *fptr;
fptr = fopen("./regist", "r");
if(fptr == NULL) //if file does not exist, create it
{
fptr = fopen("./regist", "w");
}
fclose(fptr);
if(find_username(argv[1])){
printf("Utilizador ja existe\n");
exit(1);
}
add_user_regist(argv[1],password);
printf("Utilizador %s adicionado.\n",argv[1]);
exit(0);
}
char readbuf[250];
/* Create the FIFO if it does not exist */
umask(0);
mknod(FIFO_FILE, S_IFIFO|0666, 0);
printf("Servidor iniciado.\nEm modo de espera de mensagens\n");
fcntl(0, F_SETFL, O_NONBLOCK);
while(1){
char c = getchar();
if(c=='q' || c=='Q')
exit(0);//by now only goes out
fp = fopen(FIFO_FILE, "r");
fgets(readbuf, 250, fp);
fclose(fp);
if(readbuf[0]=='U')
user_access(readbuf);
else if(readbuf[0]=='W')
who_online(readbuf);
else if(readbuf[0]=='R'){
char* tmp;
strtok(readbuf,":");
tmp = strtok(NULL,";");
remove_online(tmp);
printf("# %s fez logout\n",tmp);
}
else if(readbuf[0]=='F'){
process_msg(readbuf);
}
}
return(0);
}
Check out NCurses, the go-to library for any kind of advanced terminal software.
Among other things, it provides you with the tools to do "raw" terminal I/O -- using
int getch( void )
, made a non-blocking call viaint nodelay( WINDOWS * win, bool bf )
.This blocks:
This doesn't (returning
ERR
if there is no input pending):Note that NCurses comes with its own set of I/O functions, which you will have to use instead of the
<stdio.h>
ones (check theprintw()
above).An alternative to termios/ncurses using GNU Readline:
Compile using
-lreadline