From 2349682970fe5bd7e11905fdf8030ef8761458be Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Fri, 27 Jan 2023 12:02:39 +0100 Subject: [PATCH] Create file with function getAvailableAccountBalance --- src/currentCustomerAccountBalance.c | 45 +++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/currentCustomerAccountBalance.c diff --git a/src/currentCustomerAccountBalance.c b/src/currentCustomerAccountBalance.c new file mode 100644 index 0000000..f11ea54 --- /dev/null +++ b/src/currentCustomerAccountBalance.c @@ -0,0 +1,45 @@ +#include "currentCustomerAccountBalance.h" + +float getAvailableAccountBalance(int user_id) { + bool keep_reading = true; + float balance = 0; + + char buffer[MAX_LENGTH]; + char stringID[MAX_LENGTH] = "ID="; + char user_id_as_string[MAX_LENGTH]; + char balance_String[MAX_LENGTH]; + + sprintf(user_id_as_string, "%d", user_id); + strcat(stringID, user_id_as_string); + + FILE *file = fopen("CustomerData.txt", "r"); + + while(keep_reading) { + fgets(buffer, MAX_LENGTH, file); + if (feof(file)) { + keep_reading = false; + } + else if(strstr(buffer, stringID)) { + fgets(buffer, MAX_LENGTH, file); + fgets(buffer, MAX_LENGTH, file); + fgets(buffer, MAX_LENGTH, file); + fgets(buffer, MAX_LENGTH, file); + strcpy(balance_String, buffer); + + char *token = strtok(balance_String, "="); + while (token != NULL) { + if (atoi(token) != 0) { + balance = atof(token); + break; + } + token = strtok(NULL, "="); + } + keep_reading = false; + } + + } + fclose(file); + + return balance; + +} \ No newline at end of file