Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your program needs to compress a text file. The file text.txt contains the text

ID: 3808526 • Letter: Y

Question

Your program needs to compress a text file. The file text.txt contains the text to be compressed and this file contains lower case letters only. The file alpha.txt contains the code for each letter You need to create a file called out.txt to store the compressed text. It is sure that all the letters in text.txt will have one and only one corresponding code in the file alpha.txt. Your program needs to achieve the indeed compression and could solve the "tail bit problem You need to submit a one-page document to introduce how these two problems are solved. To solve the "tail bit problem", you may write some additional information into the output file The content of text.txt is adbb The content of alpha txt is a 00 b 01 c 10 d 11 The content of out.txt is 5

Explanation / Answer

#include <stdio.h>
int main()
{
        char a[32],ch;
        int i;
        FILE *fp1, *fp2, *fp3;
        fp1 = fopen("text.txt","r"); // open text.txt in read mode
        fp2 = fopen("alpha.txt", "r"); // open alpha.txt in read mode
        fp3 = fopen("out.txt","a+"); // open out.txt in append mode
      
       /* read data from text.txt, alpha.txt and compress into out.txt */
        while((ch=fgetc(fp1))!=EOF)
                fputc(ch, fp3);
        while((ch = fgetc(fp2))!=EOF )
                fputc(ch, fp3);
        fclose(fp3);
       /* print the totatl number of lines in out.txt */
        system("wc -l out.txt");
        return 0;
}