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?