title: Count lines, words and characters in C
body: <p>Below is my function to count number of lines, words and characters -</p>
  
  <pre><code>void count(char* file) {
  
  int fd;
  long end=0;
  char c;
  long words=0;
  long lines=0;
  
  if((fd=open(file, O_RDONLY))&gt;0){
      end=lseek(fd, 0, SEEK_END);
      lseek(fd, 0, SEEK_SET);
      while(read(fd, &amp;c, 1)==1){
          if(c == ' ')
              words++;
          if(c == '\n') {
              lines++;
              words++;
          }
      }
  
      printf("The Number of characters in file is: %ld\n",end);   
      printf("The Number of lines in file is: %ld\n",lines);
      printf("The Number of words in file is: %ld\n",words);
  
      close(fd);
  }
  else{
      printf("Error: ",strerror(errno));
  }
  }
  </code></pre>
  
  <p>I am getting right on number of lines and characters, but wrong on number of words. As you see i am counting number of spaces, if there are multiple spaces, how to count words(I don't want to use f* functions, like fscanf with filepointer) ? How does wc command handle this ?</p>