home · contact · privacy
Fixed bug that corrupted record files.
authorChristian Heller <c.heller@plomlompom.de>
Wed, 28 Aug 2013 00:22:58 +0000 (02:22 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 28 Aug 2013 00:22:58 +0000 (02:22 +0200)
src/main.c
src/misc.c

index f8118674616aed8195a50e6502749b4ed107e11b..1f9d7f7b89c01ee591f8ce911433f9238f323be8 100644 (file)
@@ -131,7 +131,7 @@ int main(int argc, char *argv[])
         if (0 == world.interactive)
         {
             file = fopen(recordfile, "r");
-            exit_err(0 == file, &world, err_o);
+            exit_err(NULL == file, &world, err_o);
             exit_err(read_uint32_bigendian(file, &world.seed), &world, err_r);
         }
 
index 8eda126ccdfcf2621ec950c3b99f5a857980ac70..88d83165928b6f810252dc8bc91dcd741abbb3af 100644 (file)
@@ -119,7 +119,7 @@ extern void turn_over(struct World * world, char action)
     char * err_write = "Trouble in turn_over() with write_uint8() "
                        "writing to opened file 'record_tmp'.";
     char * err_close = "Trouble in turn_over() with fclose() "
-                       "closing opened file 'record_tmp'.";
+                       "closing opened file 'record'.";
     char * err_unl   = "Trouble in turn_over() with unlink() "
                        "unlinking old file 'record'.";
     char * err_move  = "Trouble in turn_over() with rename() "
@@ -128,14 +128,23 @@ extern void turn_over(struct World * world, char action)
     char * recordfile     = "record";
     if (1 == world->interactive)
     {
-        FILE * file = fopen(recordfile_tmp, "a");
-        exit_err(0 == file, world, err_open);
-        exit_err(write_uint8(action, file), world, err_write);
-        exit_err(fclose(file), world, err_close);
+        FILE * file_old = fopen(recordfile, "r");
+        FILE * file_new = fopen(recordfile_tmp, "w");
+        exit_err(0 == file_old, world, err_open);
+        char c = fgetc(file_old);
+        while (EOF != c)
+        {
+            exit_err(write_uint8(c, file_new), world, err_write);
+            c = fgetc(file_old);
+        }
+        exit_err(fclose(file_old), world, err_close);
+        exit_err(write_uint8(action, file_new), world, err_write);
+        err_close = "Trouble in turn_over() with fclose() "
+                    "closing opened file 'record_tmp'.";
+        exit_err(fclose(file_new), world, err_close);
         exit_err(unlink(recordfile), world, err_unl);
         exit_err(rename(recordfile_tmp, recordfile), world, err_move);
     }
-
     world->turn++;
     rrand_seed(world->seed * world->turn);
     struct Monster * monster;