-3

I've been working on creating a Huffman Table similar to below. enter image description here

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;
}
Rob
  • 403
  • 9
  • 19
  • 2
    https://www.tutorialspoint.com/cprogramming/c_file_io.htm – Fredrik Apr 15 '18 at 08:14
  • 1
    Did you [try this](https://www.google.com/search?q=Reading+Files+Line+by+Line+in+C+site%3Astackoverflow.com)? – alk Apr 15 '18 at 08:17
  • I've tried reading the tutorialspoint but I'm having trouble making sense of reading the file and what not. But I'm sure with the second example posted by alk I can try to examine that and comeback but can someone help me translate the line# into the ASCII code. – Rob Apr 15 '18 at 08:29

1 Answers1

1

If the files you expect to read are not huge, the simplest solution might be to simply read the whole file into memory. You can use mmap() or do it yourself with something along these lines:

fd = open(filename, O_RDONLY, 0);
fstat(fd, &st);
data = malloc(st.st_size + 1);
read(fd, data, st.st_size);
data[st.st_size] = '\0';
close(fd);

and then look through the array data for newline characters (error checking left as an exercise for the reader).

lineno = 0;
line = data;
while ((nl = strchr(line, '\n') != NULL) {
    *nl = '\0';
    process(line, ++lineno);
    line = nl + 1;
}
John Hascall
  • 9,176
  • 6
  • 48
  • 72
  • It's a Text files, I'm supposed to scan lines 32 - 127. Assign the decimal in the line to the correspond line # converted to its ASCII code and then build a huffman code table. – Rob Apr 15 '18 at 09:32