0

I am trying to do Huffman coding and want to count the frequency of characters in a file and generate a .count file with the frequencies. I am currently using a struct:

typedef struct
 {
    char content;
    int fr;
 }CharF;
int count (char * filename, CharF * freq)
{
  FILE * fptr = fopen (filename,"r");
  if (fptr == NULL)
  {
    return 0;
  }
  int count = 0;
  while (!feof (fptr))
  {
    int onechar = fgetc(fptr);
    if (onechar != EOF)
    {
      count++;
      freq[onechar].content = (char) onechar;
      freq[onechar].fr += 1;
    }
  }
  fclose(fptr);
  return count;
}

How can I generate a .count file with a frequency of characters? Currently, I am returning count which is the count of the characters in the file? How can I change my function to suit my goal and how can I write these frequencies into a file to make a .count file?

fresh42juice
  • 117
  • 6
  • 3
    [Why `while(!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Barmar Feb 22 '20 at 02:10
  • 1
    There's no need for a structure. Just use an `int` array where the index is the character and the value is the count. – Barmar Feb 22 '20 at 02:11
  • 1
    The counts of the characters are in the `freq` array. Loop through that array and write the information to the file. – Barmar Feb 22 '20 at 02:12
  • @Barmar what do you mean by using an array where the index is the character ? I didn't really understand that. Or was the next comment the clarification ? I am a little confused as to what you meant. Also, will writing the freqeuncies into a file automatically produce the .count file? – fresh42juice Feb 22 '20 at 02:52
  • 2
    When you create an entry in the array of structures, you have: `freq[onechar].content = onechar;`. To access the content for character `c`, you use `freq[c].content`, but the value returned is always `c`, which you already knew. So the content element of the structure is not needed. Which then means you could replace the structure with a simple array `int freq[256] = { 0 };` and as you read characters, `freq[onechar]++;` increments the frequency. – Jonathan Leffler Feb 22 '20 at 03:18
  • @JonathanLeffler another question if you are willing - once I have gone through the whole file and got all the freqeuncies, to write into the file why do I need to loop through the array as Barmar said ? can i not simply fwrite (freq,sizeof(char),sizeof(freq),fp) ? – fresh42juice Feb 22 '20 at 04:26
  • 1
    You could use `fwrite()` as you propose. – Jonathan Leffler Feb 22 '20 at 05:00
  • OT: regarding: `if (fptr == NULL) { return 0; }` this will cause the program to exit without telling the user what happened. Suggest: `if (fptr == NULL) { perror( "fopen failed" ); return 0; }` Also a return value of 0 usually indicates success. Suggest replacing that `return 0`; with `exit( EXIT_FAILURE );` Note: both `exit()` and `EXIT_FAILURE are exposed via the header file: `stdlib.h` – user3629249 Feb 22 '20 at 22:47

0 Answers0