title: The strange behavior of 'read' system function
body: <p>The programm I tried writing should have been able to read a string of a length not longer than 8 characters and check if such string were present in the file. I decided to use 'read' system function for it, but I've come up with a strange behavior of this function. As it's written in manual, it must return 0 when the end of file is reached, but in my case when there were no more characters to read it still read a '\n' and returned 1 (number of bytes read) (I've checked the ASCII code of the read character and it's actually 10 which is of '\n'). So considering this fact I changed my code and it worked, but I still can't understand why does it behave in this way. Here is the code of my function:</p>
  
  <pre><code>int is_present(int fd, char *string)
  {
      int i;
      char ch, buf[9];
  
      if (!read(fd, &amp;ch, 1)) //file is empty
          return 0;
  
      while (1) {
          i = 0;
          while (ch != '\n') {
              buf[i++] = ch;
              read(fd, &amp;ch, 1);
          }
          buf[i] = '\0';
          if (!strncmp(string, buf, strlen(buf))) {
              close(fd);
              return 1;
          }
          if(!read(fd, &amp;ch, 1)) //EOF reached
              break;
      }
      close(fd);
      return 0;
  }
  </code></pre>