Browse Source

Line delete function

remotes/origin/feature/update-current-customer-account-balance
fdlt3817 2 years ago
parent
commit
54ca0a053d
  1. 7
      src/CustomerData.txt
  2. 5
      src/TempData.txt
  3. BIN
      src/a.exe
  4. 95
      src/updateCustomerAccountBalance.c
  5. 9
      src/updateCustomerAccountBalance.h

7
src/CustomerData.txt

@ -1,6 +1 @@
1234=example
ID=1234
forename=Max
Surname=Mustermann
password=example
balance=0
balance=500

5
src/TempData.txt

@ -0,0 +1,5 @@
1234=example
ID=1234
forename=Max
Surname=Mustermann
balance=0

BIN
src/a.exe

95
src/updateCustomerAccountBalance.c

@ -0,0 +1,95 @@
#include "updateCustomerAccountBalance.h"
#include "currentCustomerAccountBalance.c"
void deleteLineFromFile(int line){
FILE *fptr1, *fptr2;
char file1[] ="CustomerData.txt";
char file2[] ="TempData.txt";
char curr;
int del=line, line_number = 0;
fptr1 = fopen(file1,"r");
fptr2 = fopen(file2, "w");
curr = getc(fptr1);
if(curr!=EOF) {line_number =1;}
while(1){
if(del != line_number){
putc(curr, fptr2);
curr = getc(fptr1);
if(curr =='\n') line_number++;
if(curr == EOF) break;
}else{
}
}
fclose(fptr1);
remove(file1);
fclose(fptr2);
rename(file2,"CustomerData.txt");
}
void replaceBalanceInString(float replacementBalance, int currentLine) {
deleteLineFromFile(currentLine-1);
printf("deleting from line %i. balance will be replaced with %f", currentLine, replacementBalance);
char newBalanceLine[MAX_LENGTH] = "balance=";
char balance_as_string[MAX_LENGTH];
sprintf(balance_as_string, "%g", replacementBalance); //converts replacement balance to string
strcat(newBalanceLine, balance_as_string);
printf(newBalanceLine);
char buffer[MAX_LENGTH];
FILE *file = fopen("CustomerData.txt", "w");
for(int i=0;i<currentLine;i++){
fgets(buffer, MAX_LENGTH, file);
}
fprintf(file, newBalanceLine);
fclose(file);
}
bool updateAvailableAccountBalance(int user_id, float changeInBalance){
bool keep_reading = true;
float availableBalance = 0;
char buffer[MAX_LENGTH];
char stringID[MAX_LENGTH] = "ID=";
char user_id_as_string[MAX_LENGTH];
char balance_String[MAX_LENGTH];
int currentLine=0;
sprintf(user_id_as_string, "%d", user_id); // converts user_id to string
strcat(stringID, user_id_as_string);
FILE *file = fopen("CustomerData.txt", "r+");
while(keep_reading) {
fgets(buffer, MAX_LENGTH, file);
currentLine++;
if (feof(file)) {
keep_reading = false;
}
else if(strstr(buffer, stringID)) { //found the customer
fgets(buffer, MAX_LENGTH, file);
fgets(buffer, MAX_LENGTH, file);
fgets(buffer, MAX_LENGTH, file);
fgets(buffer, MAX_LENGTH, file);
strcpy(balance_String, buffer);
currentLine+=4;
availableBalance = fetchBalanceFromBalanceString(balance_String);
replaceBalanceInString(availableBalance+changeInBalance,currentLine);
keep_reading = false;
}
}
fclose(file);
}
int main(){
updateAvailableAccountBalance(1234,500);
return 0;
}

9
src/updateCustomerAccountBalance.h

@ -0,0 +1,9 @@
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 100
bool updateAvailableAccountBalance(int user_id, float changeInBalance);
void deleteLineFromFile(int lineNumber);
void replaceBalanceInString(float replacementBalance, int currentLine);
Loading…
Cancel
Save