FILE handling fprintf

This is a discussion on FILE handling fprintf within the C Programming forums, part of the General Programming Boards category; yo. i made a program which is supposed to simulate a bank account. it prompts the user to input the ...

  1. #1
    Registered User elie123's Avatar
    Join Date
    Mar 2008
    Posts
    1

    FILE handling fprintf

    yo. i made a program which is supposed to simulate a bank account. it prompts the user to input the account number and pin code; and then matches the input with that of the file "accounts.dat". It verifies the username and password, but the file doesn't update. when you deposit or withdraw, it does the arithmetics correctly, but the file content remains the same. i tried changing the "r" in the fopen tag to "w+" and also tried "r+" tag but it still doesn't write anything. the "w+" even erases everything in the accounts.dat file.

    i wish you could find time to help. thanks!

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<conio.h>
    
    struct acct{
      char acn[6];
      char pin[5];
      float bal;};
    
    void main()
      {
      struct acct n,x[100];
      int i1,i2,q,i=0,g,h;
      float amt;
    
      FILE *fp,*fp1.*fp2;
      fp=fopen("ACCOUNTS.DAT","r+");
    
      clrscr();
    
      printf("ACCOUNT NUMBER: ");
      scanf("&#37;s",&n.acn);
      printf("PIN CODE: ");
      scanf("%s",&n.pin);
    
      while(fscanf(fp,"%s\n%f\n%s\n",&x[i].acn,&x[i].bal,&x[i].pin)!=EOF)
        {
        i1=strcmp(n.acn,x[i].acn);
        i2=strcmp(n.pin,x[i].pin);
        if(i1==0&&i2==0)
          {
          g=i;
          };
        i++;
        };
    
      if(strcmp(n.acn,x[g].acn)==0&&strcmp(n.pin,x[g].pin)==0)
        {
        printf("Access permitted.");
        printf("\n\nEnter the number which corresponds to the transaction that you want to make:\n\t1. Transaction: Deposit\n\t2. Transaction: Withdraw\n\t3. Balance Inquiry\n\tOption number: ");
        scanf("%d",&q);
    
        if(q==1)
          {
          clrscr();
          printf("You chose option number 1.\n\tTRANSACTION: DEPOSIT\n\n\tPlease enter the amount that you would like to deposit: P ");
          scanf("%f",&amt);
          x[g].bal=x[g].bal+amt;
          printf("Thankyou for depositing.\n\tYour new balance is P %10.2f", x[g].bal);
          h=i;
          i=0;
          fp1=fopen("ACCOUNTS.DAT","w");
          fp2=fopen("ACCOUNTS.DAT","a");
          for(i=0;i<h;i++)
            {
            fprintf(fp2,"\n%s\n%f\n%s\n",&x[i].acn,&x[i].bal,&x[i].pin);
            };
          getch();
          exit(1);
          }
    
        if(q==2)
          {
          clrscr();
          printf("You chose option number 2.\n\tTRANSACTION: WITHDRAW\n\n\tPlease enter the amount that you would like to withdraw: P ");
          scanf("%f",&amt);
          x[g].bal=x[g].bal-amt;
          printf("Thankyou for withdrawing.\n\tYour new balance is P %10.2f", x[g].bal);
          h=i;
          i=0;
          fp1=fopen("ACCOUNTS.DAT","w");
          fp2=fopen("ACCOUNTS.DAT","a");
          for(i=0;i<h;i++)
            {
            fprintf(fp2,"\n%s\n%f\n%s\n",&x[i].acn,&x[i].bal,&x[i].pin);
            };
          getch();
          exit(1);
          }
    
        if(q==3)
          {
          clrscr();
          printf("You chose option number 3.\n\tBALANCE INQUIRY");
          printf("Your account balance is %10.2f.",x[g].bal);
          getch();
          exit(1);
         }
    
       else
          {
          printf("Thankyou for logging in.");
         };
      }
    
      else
       {
       printf("Access denied.");
       };
    
      fclose(fp);
      getch();
      }
    Last edited by elie123; 03-07-2008 at 10:43 PM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    1. fix indentation
    2. int main(void)
    3. you can write/read same file with "r+" format - you need to call a fflush(fp) when you switch between read/write operations
    However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.
    4.
    Always check the return value to see if the pointer is NULL before performing any further operations on the file.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Mutlithreaded file handling
    By nvoigt in forum Windows Programming
    Replies: 11
    Last Post: 06-30-2005, 02:39 PM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21