From 5cee1d52280b8efc1bb79d9a58466cec0900c508 Mon Sep 17 00:00:00 2001 From: Jack-Benny Persson Date: Sun, 9 Jul 2017 06:23:16 +0200 Subject: [PATCH] Misc fixes and translations --- on-stock.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/on-stock.c b/on-stock.c index 5987d57..a124c4e 100644 --- a/on-stock.c +++ b/on-stock.c @@ -3,9 +3,11 @@ #include #include +/* Macros */ #define FILEMAXLENGTH 50 #define NAMEMAXLENGTH 30 +/* Globals */ struct myData { char name[NAMEMAXLENGTH]; @@ -13,19 +15,22 @@ struct myData float price; }; +char filename[FILEMAXLENGTH] = "storage.bin"; + +/* Function prototype */ void list(struct myData *datap, int numRec); void search(struct myData *datap, int numRec); void modify(struct myData *datap, int numRec); void delete(struct myData *datap, int numRec); int new(struct myData *datap, int numRec); + int main(int argc, char* argv[]) { int numRec; int create; int choice; int opt; - char filename[FILEMAXLENGTH] = "alloctest.bin"; if (argc != 2) { @@ -98,7 +103,7 @@ int main(int argc, char* argv[]) if ( choice == 'l' || choice == 's' || choice == 'm' || choice == 'r' ) { /* Open file in read-mode */ - FILE *fp = fopen("alloctest.bin", "rb"); + FILE *fp = fopen(filename, "rb"); /* Read in the content from the file to the structure */ fseek(fp, 0, SEEK_END); @@ -135,7 +140,7 @@ void list(struct myData *datap, int numRec) { printf("Name: %s\n", datap[i].name); printf("Quantity: %d\n", datap[i].quantity); - printf("Price: %f\n", datap[i].price); + printf("Price: %.2f\n", datap[i].price); printf("\n"); } } @@ -208,7 +213,7 @@ void modify(struct myData *datap, int numRec) } } } - FILE *newfp = fopen("alloctest.bin", "wb"); + FILE *newfp = fopen(filename, "wb"); fwrite(datap, sizeof(struct myData), numRec, newfp); fclose(newfp); } @@ -235,7 +240,7 @@ void delete(struct myData *datap, int numRec) answer = getchar(); if ( answer == 'y' ) { - FILE *newfp = fopen("alloctest.bin", "wb"); + FILE *newfp = fopen(filename, "wb"); for (int j = 0; jname, NAMEMAXLENGTH, stdin); datap->name[strcspn(datap->name, "\n")] = '\0'; - if ( strcmp(datap->name, "klar") == 0 ) + if ( strcmp(datap->name, "done") == 0 ) { fclose(fp); return 0; @@ -278,10 +280,9 @@ int new(struct myData *datap, int numRec) bytes = fwrite(datap, sizeof(struct myData), 1, fp); if (bytes != 1) { - printf("Kunde inte skriva till filen!\n"); + printf("Could not write to the file!\n"); return 1; } } } -