|
|
@ -1,51 +1,45 @@ |
|
|
|
#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; |
|
|
|
void deleteLineFromFile(const char* file_name, int line, const char* new_line){ |
|
|
|
FILE* file = fopen(file_name, "r"); |
|
|
|
if (file == NULL) { |
|
|
|
printf("Error opening file!\n"); |
|
|
|
return; |
|
|
|
} |
|
|
|
char current_string[1024]; |
|
|
|
int current_line = 1; |
|
|
|
char *temp_file_name = "temp.txt"; |
|
|
|
FILE* temp_file = fopen(temp_file_name, "w"); |
|
|
|
if (temp_file == NULL) { |
|
|
|
printf("Error creating temp file!\n"); |
|
|
|
fclose(file); |
|
|
|
return; |
|
|
|
} |
|
|
|
while (fgets(current_string, sizeof(current_string), file) != NULL) { |
|
|
|
if (current_line == line) { |
|
|
|
fprintf(temp_file, "%s", new_line); |
|
|
|
} else { |
|
|
|
|
|
|
|
fprintf(temp_file, "%s", current_string); |
|
|
|
} |
|
|
|
|
|
|
|
current_line++; |
|
|
|
} |
|
|
|
fclose(fptr1); |
|
|
|
remove(file1); |
|
|
|
fclose(fptr2); |
|
|
|
rename(file2,"CustomerData.txt"); |
|
|
|
|
|
|
|
fclose(file); |
|
|
|
fclose(temp_file); |
|
|
|
if(remove(file_name)!=0){ |
|
|
|
printf("could not remove the original file!"); |
|
|
|
} // Remove the original file |
|
|
|
if(rename(temp_file_name, file_name)!=0){ |
|
|
|
printf("could not rename!"); |
|
|
|
} // Rename the temp file to the original file |
|
|
|
} |
|
|
|
|
|
|
|
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); |
|
|
|
deleteLineFromFile("CustomerData.txt",currentLine,newBalanceLine); |
|
|
|
} |
|
|
|
|
|
|
|
bool updateAvailableAccountBalance(int user_id, float changeInBalance){ |
|
|
@ -78,12 +72,13 @@ bool updateAvailableAccountBalance(int user_id, float changeInBalance){ |
|
|
|
strcpy(balance_String, buffer); |
|
|
|
currentLine+=4; |
|
|
|
availableBalance = fetchBalanceFromBalanceString(balance_String); |
|
|
|
replaceBalanceInString(availableBalance+changeInBalance,currentLine); |
|
|
|
|
|
|
|
keep_reading = false; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
fclose(file); |
|
|
|
replaceBalanceInString(availableBalance+changeInBalance,currentLine); |
|
|
|
} |
|
|
|
|
|
|
|
int main(){ |
|
|
|