From d8f8458dee1ef5f44faffa963a24ea031d63c256 Mon Sep 17 00:00:00 2001 From: Cestmir Houska Date: Fri, 22 Apr 2011 04:28:37 +0200 Subject: [PATCH] dataio.c: Modification by zero only reads the current credit Signed-off-by: Cestmir Houska --- dataio.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/dataio.c b/dataio.c index 4dd42f8..ff28cf4 100644 --- a/dataio.c +++ b/dataio.c @@ -18,20 +18,23 @@ int modify_credit(name, price) person_data = fopen(filename, "r"); if (person_data == NULL) { - printf("Warning: Filename %s does not exist! Creating...\n", filename); + printf("Warning: Filename %s does not exist!\n", filename); credit = 0; } else { fscanf(person_data, "%i", &credit); fclose(person_data); } - credit += price; - person_data = fopen(filename, "w"); - if (person_data == NULL) { - printf("ERROR: Filename %s could not be created or overwritten!\nCheck system integrity!\n", filename); - } else { - fprintf(person_data, "%i", credit); - fclose(person_data); + // Write only if the price would change credit + if (price != 0) { + credit += price; + person_data = fopen(filename, "w"); + if (person_data == NULL) { + printf("ERROR: Filename %s could not be created or overwritten!\nCheck system integrity!\n", filename); + } else { + fprintf(person_data, "%i", credit); + fclose(person_data); + } } return credit;