Data compression in C

Asked

Viewed 537 times

0

I need to implement a C program that makes the creation of compressed files from text files (for example, C program sources, among others). This compression will be performed using the Run Length Encoding compression method. The program must have a menu with 3 options, as below:

1 - Compress -> writes a new file with compressed data 2 - Undo compression -> write a new file with uncompressed data 3 - Exit

I need the help

Start a little low, but it’s a long way down.

 #include<stdio.h>
 #include<stdlib.h>
 #include<string.h>

 int main(int argc, char *argv[]){

 int op;
 char conteudo[1110];
 FILE *pf;
 long tamanho;

do{

    system("cls");
 printf("#######################################################\n");
    printf("       |   Escolha a opcao desejada            |\n");
    printf("       | 1 - Fazer Compressao.                 |\n");
    printf("       | 2 - Desfazer a compressao             |\n");
    printf("       | 3 - Sair                              |\n");

  printf("############################################################\n");

    printf("Informe a opcao desejada: ");
    scanf("%d", &op);

    if(op != 3){
        switch(op){
            case 1:
                    pf = fopen("teste.txt", "r");

                    if(pf == NULL){
                        printf("ERRO!!");
                        getch();
                    }else{
                        fseek(pf, 0, SEEK_END);
                        tamanho = ftell(pf);
                        printf("O arquivo possui %ld bytes", tamanho);
                        getch();
                    }

                break;
            case 2:

                if(pf == NULL){
                    printf("Não foi possível abrir arquivo");
                }else{
                    fseek(pf, 0, SEEK_END);
                    tamanho = ftell(pf);

                    pf = fopen("teste.txt", "rb");
                    fread(&conteudo, sizeof(char), 1110, pf);
                    printf("\n%s", conteudo);
                    getch();
                }
                break;
            default:
                printf("Opcao invalida!\n\n\n");
                getch();
        }
    }

}while(op != 3);
fclose(pf);
}

The text the txt file can be something like

  • 1

    Dude, this code is pretty complex to post as an answer here. From a look here you have an example of how to do it. http://rosettacode.org/wiki/Run-length_encoding/C

  • oii! I even saw this example, but it doesn’t pull the data in the txt file. Have any idea of change?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.