#include <stdio.h>

FILE *ip, *op;

int color, another; 
int counter;

int main (int argc, char *argv[])
{
 
  if ((ip = fopen (argv[1], "rb")) == NULL) 
    {
      printf("Cannot open input \n", argv[1]);
      return(1);
    }
  if ((op = fopen(argv[2], "wb")) == NULL)
    {
      printf("Cannont open output \n", argv[2]);
      return(2);
    }
    
  color = fgetc (ip);
  another = fgetc (ip);
  counter = 1;

 do
   {
      if (color == another)
	{
	  counter++;
	  
	  if (counter == 120)
	    {
	      fputc(color, op);
	      //printf("color %d   ",color);
	      fputc(counter, op);
	      //printf("counter %d \n", counter);
	      color = another;
	      another = fgetc(ip);
	      counter = 1;
	    }
	  
	  another = fgetc(ip);
	  
	} else
	  {
	    fputc(color, op);
	    //printf("color %d   ",color);
	    fputc(counter, op);
	    //printf("counter %d \n", counter);
	    color = another;
	    another = fgetc(ip);
	    counter = 1;
	  }
      
   } while ((color!=EOF) | (another!=EOF));
 
 printf("DONE \n");
 fclose(ip);
 fclose(op);
 return(0);
}
