Why won't my code append data to file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jerryandchandan
    New Member
    • Dec 2008
    • 6

    Why won't my code append data to file

    i created an atm machine but the problem now is that it overwrites the file that contains the pin number information. for example,

    printf("Enter pin");
    scanf("%d",&pin );
    inputfile=fopen ("cus_info","r" );

    lets say the text contains: 101 400
    102 400
    103 400
    400 is the balance
    i enter 101 and desposit $2 the balance will be $402 but if i try again to put 2 my pin will be 2 now and the other contents is not there only the one i used.
    please help!
  • vmpstr
    New Member
    • Nov 2008
    • 63

    #2
    Post your code please.

    It seems that the problem you have is you just read what you need, and then write it back to the file (overwriting much of everything else).

    What you need to have is to read all of the entries, modify the required ones, and write all entries back

    Comment

    • jerryandchandan
      New Member
      • Dec 2008
      • 6

      #3
      //Author: Jerry Ebanks
      //Name of Program: ATM Exam(CIS4)
      /*
      Purpose:The ATM machine can perform three basic functions:1)Dep osit; 2) Withdraw; and 3)View current balance
      *1) After each deposit and withdrawal the system should display the amount deposited/withdrawn and
      *the Current Balance.
      *2) Display the current balance if option (3) Statement is chosen.
      */
      //Date 10th/12.08

      #include <stdio.h>
      #include<stdlib .h>
      int main()
      {
      // Defining Variables.

      FILE *inputfile = NULL;
      int count,test=0;
      int pin,balance;
      int customer_pin[20],customer_balan ce[20];
      int option,pin_numb er,transaction;
      int deposit,deposit _action,withdra w,withdraw_acti on;

      option = 3;
      while(option!=2 )
      {
      printf("Jerry's ATM\n");
      printf("------------------------------\n\n");
      printf("Main Menu\n");
      printf("------------------------------\n");
      printf("Chose your option:\n");
      printf("1) To Enter Pin Number\n");
      printf("2) To Exit ATM \n");
      printf("Press enter after choosing an option.\n");
      printf("------------------------------\n\n");
      scanf("%d", &option);

      // Make a simple decision.

      if (option == 1)
      {
      printf("Please enter your pin number:");
      scanf("%d", &pin_number) ;

      inputfile = fopen("customer _information.tx t", "r");

      if (inputfile == NULL)
      {
      printf("Error opening names file.\n");
      return 1;
      } //endif

      count = 0;
      while ( fscanf(inputfil e, "%d" "%d", &pin, &balance ) == 2 )
      {
      count++;
      customer_pin[count] =pin;
      customer_balanc e[count] =balance;
      if (customer_pin[count] == pin_number)
      {
      while(transacti on!=4)
      {
      system ("cls");
      printf("ATM Transactions\n" );
      printf("------------------------------\n");
      printf("1)to Deposit\n2)To Withdraw\n3)Vie w Current balance\n4)To Exit \n");
      printf("Press enter after making a choice.\n");
      printf("------------------------------\n");
      scanf("%d",&tra nsaction);

      if(transaction= =1)
      {
      system("cls");
      printf("Deposit \n");
      printf("------------------------------\n\n");
      printf("Please enter the amount of money you would like to deposit: ");
      scanf("%d", &deposit);
      deposit_action= deposit + balance;
      inputfile=fopen ("customer_info rmation.txt","w ");

      if (inputfile == NULL)
      {
      printf("Unable to open input file.\n");
      return 1;
      }
      printf("The amount of money that was deposited is %d and your new balance is %d\n",deposit,d eposit_action);
      fprintf(inputfi le,"%d %d",deposit,dep osit_action);
      fclose(inputfil e);
      system("pause") ;
      }

      if(transaction= =2)
      {
      system("cls");
      printf("Withdra w\n");
      printf("------------------------------\n\n");
      printf("Please enter the amount of money you would like to withdraw: ");
      scanf("%d", &withdraw);
      withdraw_action =balance-withdraw;
      inputfile=fopen ("customer_info rmation.txt","w ");

      if (inputfile == NULL)
      {
      printf("Unable to open input file.\n");
      return 1;
      }
      printf("The amount of money that was withdraw is %d and your balance is %d\n",withdraw, withdraw_action );
      fprintf(inputfi le,"%d %d",withdraw,wi thdraw_action);
      fclose(inputfil e);
      system("pause") ;

      }
      else
      {
      if(balance==0)
      {
      printf("Insuffi cient balance.Balance is %d.\n",balance) ;
      fprintf(inputfi le,"%d",balance );
      inputfile=fopen ("customer_info rmation.txt","r ");

      if (inputfile == NULL)
      {
      printf("Unable to open input file.\n");
      return 1;
      }
      fclose(inputfil e);

      }

      }

      if(transaction= =3)
      {
      system("cls");
      printf("Viewing Current Balance\n");
      printf("------------------------------\n\n");
      inputfile=fopen ("customer_info rmation.txt","r ");

      if (inputfile == NULL)
      {
      printf("Unable to open input file.\n");
      return 1;
      }
      printf("Your balance is %d.\n\n",balanc e);
      fprintf(inputfi le,"%d",balance );
      fclose(inputfil e);
      system("pause") ;
      }


      if(transaction= =4)
      {
      return 0;
      }
      }
      }
      }
      }
      }
      return 0;
      }

      Comment

      • jerryandchandan
        New Member
        • Dec 2008
        • 6

        #4
        could tell others to help me please.

        Comment

        • newb16
          Contributor
          • Jul 2008
          • 687

          #5
          Yes, you can't read from text file using fscanf and write to it in the same time ( until input file is closed ). May be you should separate reading (open file, fscanf all it s contents into memory, close ), updatting and writing ( fprintf all stuff back )

          Comment

          • Tassos Souris
            New Member
            • Aug 2008
            • 152

            #6
            Well it is not true that you can not write and read to a file without having to do the open/close operations.
            You open your file with "r" mode. That means that an input stream is associated with the file. That means, of course, that you can only read from the file.
            When you open a file with "w" mode, for example, an output stream is associated with the file. That means that you can only write to the file.
            What you want is a stream that you can both read and write to. That is an update stream. To open a file and have an update stream associated with it you must include the '+' character in the mode parameter of the fopen function.
            There are however some things to consider, such as that input cannot be followed by output without an intervening call to a file positioning function (unless the input operation encounters end of file), and that output cannot be followed by input without an intervening call to the fflush() function or a file positioning function (fseek, fsetpos or rewind). There are some other subtle things too.
            You should probably check more carefully the fopen function and how it works. See what's the meaning of the second argument to the function. If you have any questions simply ask!! :-)

            Hope i helped!

            Comment

            Working...