简体   繁体   中英

C Function to count the frequency of characters in a file

Little background, I am writing a C program to encode and decode files with Huffman code. I'm having trouble getting my function to count frequencies correctly (it's way off), and I'm not sure why. The program takes two command line arguments, the input file and the output file. The function in question also takes the same arguments and returns a pointer to an unsigned long array for later use. The min and max variables assign their ending values to global variables for later use as well. Any help would be greatly appreciated!

unsigned long* printFreqTable(FILE* in, FILE* out)
{
  int i, j, k;
  unsigned long tCount = 0;
  unsigned long max = 0;
  int maxIndex;
  unsigned long min;
  int minIndex;
  /*unsigned long minControl;*/
  unsigned long freq[256];
  unsigned long* freqPtr;
  char c;
  printf("Entered printFreqTable()\n");
  while((c = getc(in)) != EOF)
    {
      if((c-'0') <= 255) {freq[c-'0']++;}
      if(freq[c-'0'] > max)
    {
      max = freq[c-'0'];
      maxIndex = c-'0';
    }
    }
  min = max;
  for(i = 0; i < 33; i++)
    {
      tCount += freq[i];
      if(freq[i] != 0) {fprintf(out, "\n=%d\t\t%2lu", i, freq[i]);}
      if(freq[i] < min && freq[i] > 0)
    {
      min = freq[i];
      minIndex = i;
    }
    }
  for(j = 33; j < 127; j++)
    {
      tCount += freq[j];
      if(freq[j] != 0) {fprintf(out, "\n%c\t\t%2lu", j, freq[j]);}
      if(freq[j] < min && freq[j] > 0)
    {
      min = freq[j];
      minIndex = j;
    }
    }
  for(k = 127; k < 256; k++)
    {
      tCount += freq[k];
      if(freq[k] != 0) {fprintf(out, "\n=%d\t\t%2lu", k, freq[k]);}
      if(freq[k] < min && freq[k] > 0)
    {
      min = freq[k];
      minIndex = k;
    }
    }
  freqPtr = freq;
  minFreqIndex = minIndex;
  maxFreqIndex = maxIndex;
  totalChars = tCount;
  fprintf(out, "\n\nTotal number of characters in file %lu", tCount);
  printf("Finished printFreqTable()\n");
  return freqPtr;
}

This is the input

a small sample string

This is the output


1        3
5        1
7        1
9        1
<        3
=        2
>        1
@        1
B        1
C        3
D        1
=159        -573673472
=160        1050929936
=161        1050929168
=162        1050930168
=163         2
=164        66306
=165        -575651989
=167        1994752
=168        1993312
=169        1993312
=171         5
=172        4091904
=173        4116480
=174        4114528
=175        4131552
=176        1994752
=177         3
=203        8192
=204        1178
=207        -573406864
=208         1
=209        -575568105
=212         9
=213        -575630015
=215        31
=216        -573403696
=217        47
=218        1331192865
=222        1050929936
=223        -575630898
=224        -573673472
=225        1050929936
=226        2147479968
=227        -575650745
=228         1
=229         0
=230        1050929120
=231        -575608588
=232        -575681824
=233        -579717286
=235        -575643587
=236        1050930096
=237         1
=238        1050929120
=239        10896419
=240        -573406912
=241        -573403696
=242         0
=243        1331192865
=244        66306
=245        10896419
=246         1
=247        33261
=250        2030928
=251        4096
=252        3968
=253        1638664235
=254        421271146
=255        1607359089

Total number of characters in file 3418115080132923

So as pointed out by @chux-ReinstateMonica, I didn't instantiate the array and I was needlessly using "c-'0'" for the index. I have since changed this and it fixed the issue, thank you to all those that took the time to help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM