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

struct table1 {
  char dtype;
  char flag1;
  char flag2;
  char quad;
  short lat;
  short lon;
  int date;
  short time;
  short sta;
  int cruise;
  char vessel[8];
};

struct table2 {
  int hoid;
  char t_prec;
  char s_prec;
  short depth;
  int temp;
  int sal;
  int cruise;
  short sta;
  char dtype;
};

main()
{
    FILE *f1 = fopen("table1.copy", "r");
    FILE *f2 = fopen("table2.copy", "r");
    struct table1 *t1 =
	(struct table1 *) calloc(3317, sizeof(struct table1));
    struct table2 *t2 =
	(struct table2 *) calloc(62000, sizeof(struct table2));
    char buf[1024];
    int n_t1, n_t2, i, j;

    for (n_t1 = 0; fgets(buf, sizeof(buf), f1); ++n_t1) {
	sscanf(buf, "%d %d %s",
	       &t1[n_t1].id,
	       &t1[n_t1].some_int,
	       &t1[n_t1].some_text[0]);
    }
    fprintf(stderr, "successfully read %d lines\n", n_t1);
    for (n_t2 = 0; fgets(buf, sizeof(buf), f2); ++n_t2) {
	sscanf(buf, "%d %d %d %s %s",
	       &t2[n_t2].id,
	       &t2[n_t2].some_int,
	       &t2[n_t2].pointer_to_a,
	       &t2[n_t2].some_text[0],
	       &t2[n_t2].another_text[0]);
    }
    fprintf(stderr, "successfully read %d lines\n", n_t2);
    for (i = 0; i < n_t1; ++i) {
	for (j = 0; j < n_t2; ++j) {
	    if (t1[i].id == t2[j].pointer_to_a &&
		t1[i].some_int == t2[j].some_int) {
		printf("%d\t%d\t%s\t%s\t%s\n",
		       t1[i].id,
		       t1[i].some_int,
		       t1[i].some_text,
		       t2[j].some_text,
		       t2[j].another_text);
		fflush(stdout);
	    }
	}
	fprintf(stderr, "done with %d\n", i);
    }
}
