Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Very simple binary file compressor in C

Very simple binary file compressor in C

Scheduled Pinned Locked Moved C / C++ / MFC
help
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stonemanhero
    wrote on last edited by
    #1

    Hello as subject says i am trying to make some simple binary file compressor (for now). I started with basics. This code opens file in binary mode reads byte by byte and input bytes writes to output "compressed" file. If byte == 0x00 counts number of 0x00's and writes 0x00 and number of 0x00's.

    #include
    #include

    int main()
    {
    int i, duzina, stanje=0, brojac=0;
    unsigned char chr;
    FILE* ulaz = fopen("../testapp/testapp", "rb");
    FILE* izlaz = fopen("../testapp/testapp.zip", "wb");

    if(ulaz == NULL)
    {
    	printf("Problem with file!\\n");
    	exit(1);
    }
    if(izlaz == NULL)
    {
    	printf("Problem with output file!\\n");
    	exit(1);
    }
    
    fseek(ulaz, 0, SEEK\_END);
    duzina = ftell(ulaz);
    fseek(ulaz, 0, SEEK\_SET);
    
    for(i=0; i
    

    To "decompress" file i use this code which as input have "compressed" file and read byte by byte if byte == 0x00 reads integer and write N times 0x00 (N value = readed integer)

    #include
    #include

    int main()
    {
    int i,j, duzina, brojac=0;
    unsigned char chr;

    FILE\* ulaz = fopen("../testapp/testapp.zip", "rb");
    FILE\* izlaz = fopen("../testapp/testapp1", "wb");
    
    if(ulaz == NULL)
    {
    	printf("Problem with file!\\n");
    	exit(1);
    }
    if(izlaz == NULL)
    {
    	printf("Problem with output file!\\n");
    	exit(1);
    }
    
    fseek(ulaz, 0, SEEK\_END);
    duzina = ftell(ulaz);
    fseek(ulaz, 0, SEEK\_SET);
    
    for(i=0; i
    

    So my problem is that when "decompressing" is finshed some bytes are missing (program works correct).

    Byte comparing:

    [stone@hero testapp]$ diff testapp1.hex testapp.hex
    443,444c443,444
    < 0001c00 0001
    < 0001c01

    0001c00 0001 0000 0000 0000 0000 0000 0000 0000
    0001c10
    [stone@hero testapp]

    CPalliniC 1 Reply Last reply
    0
    • S stonemanhero

      Hello as subject says i am trying to make some simple binary file compressor (for now). I started with basics. This code opens file in binary mode reads byte by byte and input bytes writes to output "compressed" file. If byte == 0x00 counts number of 0x00's and writes 0x00 and number of 0x00's.

      #include
      #include

      int main()
      {
      int i, duzina, stanje=0, brojac=0;
      unsigned char chr;
      FILE* ulaz = fopen("../testapp/testapp", "rb");
      FILE* izlaz = fopen("../testapp/testapp.zip", "wb");

      if(ulaz == NULL)
      {
      	printf("Problem with file!\\n");
      	exit(1);
      }
      if(izlaz == NULL)
      {
      	printf("Problem with output file!\\n");
      	exit(1);
      }
      
      fseek(ulaz, 0, SEEK\_END);
      duzina = ftell(ulaz);
      fseek(ulaz, 0, SEEK\_SET);
      
      for(i=0; i
      

      To "decompress" file i use this code which as input have "compressed" file and read byte by byte if byte == 0x00 reads integer and write N times 0x00 (N value = readed integer)

      #include
      #include

      int main()
      {
      int i,j, duzina, brojac=0;
      unsigned char chr;

      FILE\* ulaz = fopen("../testapp/testapp.zip", "rb");
      FILE\* izlaz = fopen("../testapp/testapp1", "wb");
      
      if(ulaz == NULL)
      {
      	printf("Problem with file!\\n");
      	exit(1);
      }
      if(izlaz == NULL)
      {
      	printf("Problem with output file!\\n");
      	exit(1);
      }
      
      fseek(ulaz, 0, SEEK\_END);
      duzina = ftell(ulaz);
      fseek(ulaz, 0, SEEK\_SET);
      
      for(i=0; i
      

      So my problem is that when "decompressing" is finshed some bytes are missing (program works correct).

      Byte comparing:

      [stone@hero testapp]$ diff testapp1.hex testapp.hex
      443,444c443,444
      < 0001c00 0001
      < 0001c01

      0001c00 0001 0000 0000 0000 0000 0000 0000 0000
      0001c10
      [stone@hero testapp]

      CPalliniC Online
      CPalliniC Online
      CPallini
      wrote on last edited by
      #2

      The compressor has a problem with a possibly pending 0-sequence at the end of the file, because your program outputs the 0-sequences only on changes, that is when meets a non-zero character. You have to check for a pending 0-sequence immediately after execution of the for loop:

      if(stanje != 0)
      {
      fputc(0x00, izlaz);
      fwrite(&brojac, sizeof(int),1,izlaz);
      }

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups