如何从C中的文件中读取非ascii字符?

时间:2014-05-07 14:56:51

标签: c file-io non-ascii-characters huffman-code

我现在正在读这样的内容:

    while (fscanf(in, "%c", infile) != EOF) 
    {
    ch = *infile;
    count++;
    ascii[ch]++;
    }

制作我的频率表:

    void frequency ()
    {
    unsigned long long i;
    for (i = 0; i < 255; i++)
    {
     if (ascii[i] != 0)
     {
      uniqueLetters++;
      if (i < 33)
     {
    printf("=%llu\t%lu\n", i, ascii[i]);
      }
     else if (i > 126)
  {
    printf("=%llu\t%lu\n", i, ascii[i]);
  }
  else printf("%c\t%lu\n", (int)(i), ascii[i]);
}
   } 
  printf("unique letters: %lu\n", uniqueLetters);
  }

(这是一个霍夫曼编码项目,当我尝试读取整个文件时,我完全错过126以上的任何内容......)

1 个答案:

答案 0 :(得分:1)

尝试fgetc

FILE * fp = fopen(filename, "r");

int ch; // return type of fgetc is int
while ((ch = fgetc(fp)) != EOF)
    ascii[ch]++;
相关问题