/* priklad pr8_15.c zapis do suboru nacitanim z 2 suborov
 so zadanim nazvu suboru cez parametre
 1.subor   - vytvoreny ulohou pr8_14 (p.txt)
 2.subor - ASCII, pocet+1 riadkov  (p1.txt)
 3.subor - kombinacia 1. a 2.suboru (p2.txt)  */
#include <stdio.h>
#include <string.h>
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>

#define MAX 50

void main(int argc, char *argv[])
{
  FILE *in2;
  int in1,out,lng;
  int pocet,min,max;
  float x,y;
  char line[MAX];

  if (argc < 4) printf("Volanie: %s file1 file2 file3\n",argv[0]);
  else
  {
    if ((in1 = open(*++argv,O_RDONLY)) == -1)
    {
      perror("Error ");
      printf("Can't open [%s]\n",*argv);
      exit();
    }
    if ((in2 = fopen(*++argv,"r")) == NULL)
    {
      perror("Error ");
      printf("Can't open [%s]\n",*argv);
      exit();
    }
    if((out=open(*++argv,O_CREAT|O_TRUNC,S_IWRITE)) == -1)
    {
      perror("Error ");
      printf("Can't open [%s]\n",*argv);
      exit();
    }
    read(in1,&pocet,sizeof(pocet));
    read(in1,&min,sizeof(min));
    read(in1,&max,sizeof(max));
    write(out,&pocet,sizeof(pocet));
    write(out,&min,sizeof(min));
    write(out,&max,sizeof(max));
    for(lng=0;lng<pocet+1;lng++)
    {
      fgets(line,MAX,in2);
      min=strlen(line);
      write(out,&min,sizeof(int));
      write(out,line,strlen(line));
    }
    min=0;
    while((lng=read(in1,&x,sizeof(float)))>0)
    {
      read(in1,&y,sizeof(float));
      printf("%d x=%f y=%f\n",min++,x,y);
      write(out,&x,sizeof(float));
      write(out,&y,sizeof(float));
    }
    close(in1);
    fclose(in2);
    close(out);
  }
}