
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "string.h"


struct Str{
int c;
char **items;
};


void AddStr(struct Str *stat, char *s)
{
   stat -> c ++;        
   stat -> items = realloc(stat -> items, sizeof(char*) * stat->c);
   stat -> items[stat -> c-1] = calloc(strlen(s), sizeof(char));
   strcpy(stat -> items[stat->c-1], s);
}


void FreeStr(struct Str *stat)
{
int i;
 for (i=0; i<stat->c; i++)
  free(stat->items[i]);

free(stat->items);
}


void Bubble_Sort_Increase(struct Str *m)
{
int i, r = 0;
char *temp;

while (r != -1) {
	r = -1;
	for (i=0; i < m->c -1; i++)
          if (strlen(m->items[i])>strlen(m->items[i+1]))
		{
			r = i+1;
			temp = m->items[i];
 			m->items[i] = m->items[r];
			m->items[r] = temp;
		}
 }
}


int main(int argc, char* argv[])
{

FILE *f;
struct Str stat;
char *s;
int i;

//-----------
stat.c = 0;
stat.items = malloc(sizeof(char*));
//-----------


 if (argv[1] == NULL)
 {
  printf("Incorrect input peremeters!");
  return -1;
 }

 f = fopen(argv[1], "r");

 if (f == 0)  {
    printf("No file");
    getch();
    return -2;
 }


  while (!feof(f))
   {
      fscanf(f, "%s", s);
      AddStr(&stat, s);
   }

 Bubble_Sort_Increase(&stat);

  fclose(f);
  f = fopen("1.txt", "w");

   for (i=0; i < stat.c; i++)
    fprintf(f,"%s\n", stat.items[i]);


 fclose(f);

 getch();

//--------------
FreeStr(&stat);
//--------------
 return 0;
}

