diff --git a/src/CustomerData.txt b/src/CustomerData.txt index 782579a..05ec80e 100644 --- a/src/CustomerData.txt +++ b/src/CustomerData.txt @@ -1 +1,6 @@ -balance=500 \ No newline at end of file +1234=example +ID=1234 +forename=Max +Surname=Mustermann +password=example +balance=1000 \ No newline at end of file diff --git a/src/TempData.txt b/src/TempData.txt deleted file mode 100644 index f81d5a8..0000000 --- a/src/TempData.txt +++ /dev/null @@ -1,5 +0,0 @@ -1234=example -ID=1234 -forename=Max -Surname=Mustermann -balance=0 diff --git a/src/a.exe b/src/a.exe index 25d0c58..4083994 100644 Binary files a/src/a.exe and b/src/a.exe differ diff --git a/src/updateCustomerAccountBalance.c b/src/updateCustomerAccountBalance.c index 48ad5e8..6ee64e9 100644 --- a/src/updateCustomerAccountBalance.c +++ b/src/updateCustomerAccountBalance.c @@ -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; - }else{ - - } - +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