title: How to count multiple characters from a file in C language using POSIX functions?
body: <p>I'm trying to write a program that takes a file and a string by using standard POSIX functions, program counts all the characters in file which the string contains.</p>
  
  <p>For example if the user writes:</p>
  
  <pre><code>count.exe x.txt abcd
  </code></pre>
  
  <p>The program calculates the number of each character: a, b, c, d in file x.txt</p>
  
  <p>Sample message:</p>
  
  <pre><code>Number of 'a' characters in 'x.txt' file is: 4
  Number of 'b' characters in 'x.txt' file is: 9
  Number of 'c' characters in 'x.txt' file is: 7
  Number of 'd' characters in 'x.txt' file is: 0
  </code></pre>
  
  <p>The code that I got so far:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;errno.h&gt;
  
  #define BUFSIZE     1024
  
  
  void exit_sys(const char* msg)
  {
      perror(msg);
      exit(EXIT_FAILURE);
  }
  
  void exit_fail(const char* msg)
  {
      fprintf(stderr, "%s\n", msg);
      exit(EXIT_FAILURE);
  }
  
  int get_count(char* p, size_t size, char c)
  {
      int count = 0;
      size_t i;
  
      for (i = 0; i &lt; size; ++i)
          if (p[i] == c)
              ++count;
  
      return count;
  }
  
  
  void run_count_characters_application(int argc, char** argv)
  {
      int fd;
      char c;
      char buf[BUFSIZE];
      int n;
      int count;
  
      if (argc != 3)
          exit_fail("usage: ./mycounter file character");
  
      if (strlen(argv[2]) &lt; 0)
          exit_fail("You have to give at least one character");
  
      c = argv[2][0];
  
      if ((fd = open(argv[1], O_RDONLY)) &lt; 0)
          exit_sys("open");
  
      count = 0;
  
  
      while ((n = read(fd, buf, BUFSIZE)) &gt; 0)
          count += get_count(buf, n, c);
  
      if (n &lt; 0)
          exit_sys("read");
  
  
      printf("Count:%d\n", count);
  
      close(fd);
  }
  
  int main(int argc, char** argv)
  {
      run_count_characters_application(argc, argv);
  
      return 0;
  }
  </code></pre>
  
  <p>The problem with what I got so far in this code is that it only counts one character (only the first character), I want to know how to make it read and count the other characters that I write in the command, thank you in advance :) </p>