title: Making stdin non-blocking
body: <p>I have an exercise where I am required to print a file slowly (1 second intervals) until the file ends, unless the user types a character.</p>
  
  <p>So far, the program outputs the file in one second intervals which is great, but when I type a character, nothing happens.  My guess is that I am using select wrong somehow.</p>
  
  <p>This is the final program I ended up submitting.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/select.h&gt;
  #include &lt;sys/time.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  
  int main(void)
  {
      FILE* infile;
      char str[100];
      fd_set readset;
      struct timeval tv;
  
      // open a file
      if((infile = fopen("infile", "r")) == NULL)
      {
          (void)printf("Couldn't open the file\n");
          exit(1);
      }
      // file was opened successfully
      else
      {       
          // while we are not at the end of a file
          while(fgets(str, 100, infile) != NULL)
          {
              FD_ZERO(&amp;readset);
              FD_SET(fileno(stdin), &amp;readset);
              // set the time value to 1 second
              tv.tv_sec = 1;
              tv.tv_usec = 0;
              select(fileno(infile)+1, &amp;readset, NULL, NULL, &amp;tv);
              // the user typed a character so exit
              if(FD_ISSET(fileno(stdin), &amp;readset))
              {
                  fclose(infile);
                  exit(0);
              }
              // the user didn't type a character so print the next line
              else
              {
                  fgets(str, 100, stdin);
                  puts(str);
              }
          }
  
          // clean up
          fclose(infile);
      }
  
      // report success
      return 0;
  }
  </code></pre>
  
  <p>Thanks for the help!</p>