title: Why doesn't mbtowc count character set as expected?
body: <p>I want count the characters (in various charsets) in a file and I'm using the function 'mbtowc' to detect the characters. I cannot figure out why the chars and results values are different. Here is my example:</p>
  
  <pre><code>char buf[BUFFER_SIZE + MB_LEN_MAX];
  
  int fd = open ("chinese_test", O_RDONLY);
  
  unsigned int bytes, chars;
  
  int bytes_read;
  
  bytes = chars = 0;
  
  while((bytes_read = read(fd, buf, BUFFER_SIZE)) &gt; 0) {
      wchar_t wc_buf[BUFFER_SIZE], *wcp;
      char *p;
      int n = 0;
  
      bytes += bytes_read;
  
      p = buf;
      wcp = wc_buf;
  
      while((n = mbtowc(wcp, p, MB_LEN_MAX)) &gt; 0) {
          p += n;
          wcp++;
  
          chars++;
      }
  
  }
  
  printf("chars: %d\tbytes: %d\n", chars, bytes);
  </code></pre>
  
  <p>I test the function with a text with some GB2312 characters, but <em>chars</em> and <em>bytes</em> are too different values.</p>
  
  <p>My test returns -> chars: 4638 | bytes: 17473
  but 'wc' linux command returns: chars: 16770 | bytes: 17473</p>
  
  <p>Why this difference? What did I do wrong?</p>
  
  <hr>
  
  <p>Now I've this code but there are still soe difference in the result.    </p>
  
  <pre><code>char buf[BUFFER_SIZE * MB_LEN_MAX];
  
  int fd = open ("test_chinese", O_RDONLY), filled = 0;
  
  unsigned int bytes, chars;
  
  int bytes_read;
  
  bytes = chars = 0;
  
  while((bytes_read = read(fd, buf, BUFFER_SIZE)) &gt; 0) {
      wchar_t wc_buf[BUFFER_SIZE], *wcp;
      char *p;
      int n = 0;
  
      bytes += bytes_read;
  
      p = buf;
      wcp = wc_buf;
  
  
  
      while(bytes_read &gt; 0) {
          n = mbtowc(NULL, p, MB_LEN_MAX);
  
          if (n &lt;= 0) {
              p++;
              bytes_read--;
              continue;
          }
          p += n;
  
          bytes_read -= n;
  
          chars++;
      }
  
  }
  
  printf("\n\nchars: %d\tbytes: %d\n", chars, bytes);
  </code></pre>