Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Reading a number from a file and adding another number onto the file.

Reading a number from a file and adding another number onto the file.

Reading a number from a file and adding another number onto the file.

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:

#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;

printf("Enter the account number: \n");
scanf("%d", &accnum);

itoa(accnum, account, 10);

printf("Enter amount you'd like to send \n");
scanf("%d", &amount);


printf("Are you sure you would like to continue? (Y/N) \n");
scanf("%c", &choice);
if ((choice == 'Y') || (choice == 'y'))

fp=fopen(fpath, "w+");

fprintf(fp, "%d", amount);

printf(" \n Transaction complete... \n");
printf("\n Returning to menu \n");

printf("Cancelling... \n");

Thanks! :)

RE: Reading a number from a file and adding another number onto the file.

Hi Ethan1812,
I'm not sure if it's what you need, but IMO:

If you have a file (your bank bank) with accounts and on every account there is an amount of money,
so in your file it will look something like this:


123         0.55
XYZ      1000.00
456       125.50 

Now, if you want to send e.g. amount of 100.- to account XYZ, then you need to do it in these steps:

1. read all lines of the file into a data structure, for example an array (or linked list)


acc_array = {{123, 0.55}, {XYZ, 1000.00}, {456, 125.50}} 

2. then search in the array for the given account, in this case it's this element:


acc_array[1] = {XYZ, 1000.00} 

and add the 100.00 to the existing amount, i.e. you will get


acc_array[1] {XYZ, 1100.00} 

3. At end, rewrite the file so, that you write again all array elements as lines into the file, i.e. you will get


123         0.55
XYZ      1100.00
456       125.50 

RE: Reading a number from a file and adding another number onto the file.

Thanks for the help I'll try that!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close