+ /* grid size is the first field */
+ fprintf (outfile, "%d\n", puzzle->grid_size);
+ /* whether grid is frozen or not */
+ fprintf (outfile, "%d\n", puzzle->grid_frozen);
+ /* the hashed password */
+ fprintf (outfile, "%s\n", puzzle->hashed_password);
+ /* the salt */
+ fprintf (outfile, "%s\n", puzzle->salt);
+
+ /* First output the grid characters columns/rows */
+ for (int i = 0; i < puzzle->grid_size; i ++)
+ {
+ for (int j = 0; j < puzzle->grid_size; j ++)
+ fprintf (outfile, "%c", puzzle->chars[i][j]);
+ fprintf (outfile, "\n");
+ }
+
+ /* Next output the start across/down numbers */
+ for (int i = 0; i < puzzle->grid_size; i ++)
+ {
+ for (int j = 0; j < puzzle->grid_size; j++)
+ {
+ fprintf (outfile, "%d ", puzzle->start_across_word[i][j]);
+ fprintf (outfile, "%d ", puzzle->start_down_word[i][j]);
+ }
+ fprintf (outfile, "\n");
+ }
+
+ /* Output the across clues */
+ fprintf (outfile, "ACROSS\n");
+ /* Search the grid for across words */
+ for (int i = 0; i < puzzle->grid_size; i ++)
+ {
+ for (int j = 0; j < puzzle->grid_size; j++)
+ {
+ /* if it is an across word, then put the word index followed by
+ tab character (as separator) and the clue */
+ if (puzzle->start_across_word[i][j] != -1)
+ fprintf (outfile, "%d\t%s\n", puzzle->start_across_word[i][j],
+ puzzle->clue_across[i][j]);
+ }
+ }
+
+ /* Output the down clues */
+ fprintf (outfile, "DOWN\n");
+ /* Search the grid for down words */
+ for (int i = 0; i < puzzle->grid_size; i ++)
+ {
+ for (int j = 0; j < puzzle->grid_size; j++)
+ {
+ /* same as across word, put the word index followed by the tab
+ character and then the clue */
+ if (puzzle->start_down_word[i][j] != -1)
+ fprintf (outfile, "%d\t%s\n", puzzle->start_down_word[i][j],
+ puzzle->clue_down[i][j]);
+ }
+ }
+
+ /* Flush the buffer and rewind to beginning - to read and save into
+ gzip compressed file */
+ fflush (outfile);
+ fseek (outfile, 0, 0);
+
+ /* now compress the file and save it to destination file */
+ gzFile outdestfile = gzopen (file, "wb");
+ if (outdestfile == NULL)
+ {
+ fprintf (stderr, "%s\n", ERROR_WRITING_FILE);
+ fclose (outfile);
+ exit (1);
+ }
+ char buf[128];
+ int num = fread (buf, sizeof(char), sizeof(char)*128, outfile);
+ while (num > 0)
+ {
+ int res = gzwrite (outdestfile, buf, num*sizeof(char) );
+ if (res == 0)
+ {
+ fprintf (stderr, "%s %s\n", ERROR_WRITING_FILE, COMPRESSED);
+ fclose (outfile);
+ exit (1);
+ }
+ num = fread (buf, sizeof(char), sizeof(char)*128, outfile);
+ }
+ gzclose (outdestfile);