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

#define FL "test.dat"

main() {
FILE *infile, *outfile1, *outfile2;
int n=0, opn=0;
char help[1000], hilfe[100];
char h[3][100], n1[100], n2[100], m1[5], m2[5], d1[100], d2[100];

if ((infile=fopen(FL, "r"))==NULL) {
   printf("Can not open file %s\n", FL);
   perror("OPEN");
   exit(0);
   }
m1[0]=0; m2[0]=0;

while (fgets(help, 80, infile)>0) {
   if (n<2) { 
      strcpy(h[n], help);
      n++;
      }
   else {
      if (n==2) {
          n=10;
          }
      sscanf(help, "%s %s", n1, n2);
      if (n==3) {
          if ((outfile1=fopen(m1, "w"))==NULL) {
             perror("OPEN");
             exit(0);
             }
          if ((outfile2=fopen(m2, "w"))==NULL) {
             perror("OPEN");
             exit(0);
             }
          opn=1;
             fprintf(outfile1, "%s	", n1);
             fprintf(outfile2, "%s	", n2);
          n++;
          }
      else {
          if (strncmp(help, "    ", 4)==0) {
             m1[0]=n1[1]; m1[1]=n1[2]; m1[2]=0;
             m2[0]=n2[1]; m2[1]=n2[2]; m2[2]=0;             
             printf("Nasao: %s %s\n", m1, m2);
             n=3;
             if (opn) {
                fclose(outfile1);
                fclose(outfile2);
                }
             opn=0;
             }
          else if (opn) {
             fprintf(outfile1, "%s	", n1);
             fprintf(outfile2, "%s	", n2);
             }
          }
      }
   }

if (opn) {
   fclose(outfile1);
   fclose(outfile2);
   }

fclose(infile);

}

