I've been working on creating a Huffman Table similar to below.
What I'm trying to do Is I have some C code that does translate an array of letters and an array of their frequencies, I'm attempting to now read a file and convert the line numbers of the file be translated into the matching ASCII code (Line 32 will convert to ASCII Code 32) and take the contents of the Line. I'm newer to C and have not yet explored taking files, I could use some tips on essentially.
- Loading a File
- Reading File Line by line and assigning numeric contents to array
- And grabbing the line # and then translating that to the matching ASCII code.
If needed I can post my code but I'm more confused as to grabbing the file as my input.
int main(int argc, char* argv[])
{
char arr[127];
int freq[127];
int num_of_line = 127;
int size = sizeof(arr) / sizeof(arr[0]);
FILE *fp = fopen("kjvdist.txt","r");
if(fp == NULL){
printf("The File Does not exist, Try Again?\n");
return;
}
char *ptr[num_of_line];
for(int row = 0; row < num_of_line; row++) {
ptr[row] = malloc(size);/*allocate memory */
fgets(ptr[row],sizeof(ptr[row]),fp);
int index = ptr[row];
char asc = ptr[row];
arr[index] = asc; /* do operation here on ptr[row] to get equivalent ASCII code */
for(int col = 0; ptr[row][col] ;col++) {
if(ptr[row][col]>='0' && ptr[row][col]<='9')//check for alphabets & other also
int ascii = ptr[row][col] + 48;
/* something like above you need to do */
}
}
HuffmanCodes(arr, freq, size);
return 0;
}