Hello, I'm trying to make a program that simulates a bank machine and the function I'm trying to do is one that sends money to someone else's account. I have made it so it can create a file and add money to it, I'll change it back to r+ later when I make the other modules. But because it overwrites whatever is in the file, I want to know how I can scan for the money in their account then add the amount onto the number in the file. Here is my code:
[tt]#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char fpath[20] = {"C:\\Acc\\"};
char account[20] = {0};
int accnum;
FILE *fp;
int main()
{
int amount = 0;
char choice;
int number = 0;
int i;
system("cls");
printf("Enter the account number: \n");
fflush(stdin);
scanf("%d", &accnum);
itoa(accnum, account, 10);
printf("Enter amount you'd like to send \n");
fflush(stdin);
scanf("%d", &amount);
strcat(fpath,account);
strcat(fpath,".txt");
printf("Are you sure you would like to continue? (Y/N) \n");
fflush(stdin);
scanf("%c", &choice);
if ((choice == 'Y') || (choice == 'y'))
{
fp=fopen(fpath, "w+");
fprintf(fp, "%d", amount);
fclose(fp);
printf(" \n Transaction complete... \n");
printf("\n Returning to menu \n");
system("Pause");
//menu
}
else
{
printf("Cancelling... \n");
//menu;
}
}
[/tt]
Thanks!
[tt]#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char fpath[20] = {"C:\\Acc\\"};
char account[20] = {0};
int accnum;
FILE *fp;
int main()
{
int amount = 0;
char choice;
int number = 0;
int i;
system("cls");
printf("Enter the account number: \n");
fflush(stdin);
scanf("%d", &accnum);
itoa(accnum, account, 10);
printf("Enter amount you'd like to send \n");
fflush(stdin);
scanf("%d", &amount);
strcat(fpath,account);
strcat(fpath,".txt");
printf("Are you sure you would like to continue? (Y/N) \n");
fflush(stdin);
scanf("%c", &choice);
if ((choice == 'Y') || (choice == 'y'))
{
fp=fopen(fpath, "w+");
fprintf(fp, "%d", amount);
fclose(fp);
printf(" \n Transaction complete... \n");
printf("\n Returning to menu \n");
system("Pause");
//menu
}
else
{
printf("Cancelling... \n");
//menu;
}
}
[/tt]
Thanks!