static const char *program_name = NULL;
 static int debug = 0;
 
+/* File of i386 opcode and register tables.  */
+static FILE *table;
+
 static void
 fail (const char *message, ...)
 {
   char *cpu_flags, *opcode_modifier, *operand_types [MAX_OPERANDS];
 
   if (fp == NULL)
-    fail (_("can't find i386-opc.tbl for reading\n"));
+    fail (_("can't find i386-opc.tbl for reading, errno = %s\n"),
+         strerror (errno));
 
-  printf ("\n/* i386 opcode table.  */\n\n");
-  printf ("const template i386_optab[] =\n{\n");
+  fprintf (table, "\n/* i386 opcode table.  */\n\n");
+  fprintf (table, "const template i386_optab[] =\n{\n");
 
   while (!feof (fp))
     {
       switch (p[0])
        {
        case '#':
-         printf ("%s\n", p);
+         fprintf (table, "%s\n", p);
        case '\0':
          continue;
          break;
            }
        }
 
-      printf ("  { \"%s\", %s, %s, %s, %s,\n",
-             name, operands, base_opcode, extension_opcode,
-             cpu_flags);
+      fprintf (table, "  { \"%s\", %s, %s, %s, %s,\n",
+              name, operands, base_opcode, extension_opcode,
+              cpu_flags);
 
-      printf ("    %s,\n", opcode_modifier);
+      fprintf (table, "    %s,\n", opcode_modifier);
 
-      printf ("    { ");
+      fprintf (table, "    { ");
 
       for (i = 0; i < ARRAY_SIZE (operand_types); i++)
        {
              || *operand_types[i] == '0')
            {
              if (i == 0)
-               printf ("0");
+               fprintf (table, "0");
              break;
            }
 
          if (i != 0)
-           printf (",\n      ");
+           fprintf (table, ",\n      ");
 
-         printf ("%s", operand_types[i]);
+         fprintf (table, "%s", operand_types[i]);
        }
-      printf (" } },\n");
+      fprintf (table, " } },\n");
     }
 
-  printf ("  { NULL, 0, 0, 0, 0, 0, { 0 } }\n");
-  printf ("};\n");
+  fclose (fp);
+
+  fprintf (table, "  { NULL, 0, 0, 0, 0, 0, { 0 } }\n");
+  fprintf (table, "};\n");
 }
 
 static void
   char *reg_name, *reg_type, *reg_flags, *reg_num;
 
   if (fp == NULL)
-    fail (_("can't find i386-reg.tbl for reading\n"));
+    fail (_("can't find i386-reg.tbl for reading, errno = %s\n"),
+         strerror (errno));
 
-  printf ("\n/* i386 register table.  */\n\n");
-  printf ("const reg_entry i386_regtab[] =\n{\n");
+  fprintf (table, "\n/* i386 register table.  */\n\n");
+  fprintf (table, "const reg_entry i386_regtab[] =\n{\n");
 
   while (!feof (fp))
     {
       switch (p[0])
        {
        case '#':
-         printf ("%s\n", p);
+         fprintf (table, "%s\n", p);
        case '\0':
          continue;
          break;
       /* Find reg_num.  */
       reg_num = next_field (str, &str);
 
-      printf ("  { \"%s\", %s, %s, %s },\n",
-             reg_name, reg_type, reg_flags, reg_num);
+      fprintf (table, "  { \"%s\", %s, %s, %s },\n",
+              reg_name, reg_type, reg_flags, reg_num);
     }
 
-  printf ("};\n");
+  fclose (fp);
+
+  fprintf (table, "};\n");
 
-  printf ("\nconst unsigned int i386_regtab_size = ARRAY_SIZE (i386_regtab);\n");
+  fprintf (table, "\nconst unsigned int i386_regtab_size = ARRAY_SIZE (i386_regtab);\n");
 }
 
 /* Program options.  */
       fail (_("unable to change directory to \"%s\", errno = %s\n"),
            srcdir, strerror (errno));
 
-  printf ("/* This file is automatically generated by i386-gen.  Do not edit!  */\n");
-  printf ("/* Copyright 2007  Free Software Foundation, Inc.\n\
+  table = fopen ("i386-tbl.h", "w");
+  if (table == NULL)
+    fail (_("can't create i386-tbl.h, errno = %s\n"), strerror (errno));
+
+  fprintf (table, "/* This file is automatically generated by i386-gen.  Do not edit!  */\n\
+/* Copyright 2007  Free Software Foundation, Inc.\n\
 \n\
    This file is part of the GNU opcodes library.\n\
 \n\
   process_i386_opcodes ();
   process_i386_registers ();
 
+  fclose (table);
+
   exit (0);
 }