How to do nonblocking input from stdin in C
<p>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.</p>

<p>For this I tryed use <code>scanf</code> of an character with </p>

<p><code>fcntl(0, F_SETFL, O_NONBLOCK);</code> </p>

<p>and </p>

<p><code>while(feof(stdin))</code> </p>

<p>but it doesn't work as expected. </p>

<p>I have searched and in some places someone says to use <code>select</code> but I can't find how to use it.</p>

<p>Anyone can advice me somehow? </p>

<p>my main function to provide more information:</p>

<pre><code>int main(int argc, char *argv[])
{


 if(argc&gt;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);
}
</code></pre>