Thread: Existing field in struct to be overwritten. BINARY FILES.

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Philippines
    Posts
    7

    Existing field in struct to be overwritten. BINARY FILES.

    Hi guys. I'm having trouble with overwriting a specific field in my struct. Anyone can help me? What should I do? Please help.

    Code:
    void Deposit(FILE *fp, struct information info, char set[]){
         int x, flag;
         float deposit;
         char id[8];
         fp=fopen("account.dat","a+b");
         system("cls");
         printf("=========DEPOSIT=========");
         printf("\nEnter Account ID:(XX-XXX)");
         gotoxy(18,1);scanf("%6s",id);
         flag=0;
         if (id[2]!='-' || strspn(id,set)!=6)
             { /*format*/
               flag=1;
               system("cls");
               printf("Invalid Account# format! ");
               printf("\nPress any key to continue..");
               getch();
             } /*format*/
         
         if (flag==0)
         {
          while(fread(&info, sizeof(struct information), 1, fp) != NULL)
             {
               if(strcmp(id,info.id)==0)
              {
                system("cls");
                printf("Account# found!");
                printf("\nPress any key to continue..");
                flag=2;
                getch();
              } 
             }
         }
         if (flag==2)
         {
          system("cls");
          printf("\nEnter deposit amount: ");
          scanf("%f", &deposit);
          if (deposit <= 0)
          {
           printf("\nInvalid amount.");
           getch();
          }
          else
          {
          info.obalance+=deposit;
          fwrite(&info,sizeof(struct information),1,fp);
          }
                                        
          getch();
         }
         fclose(fp);
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    When you open your file with the "a+" file open mode you can only write to the end of the file. You may want to study the documentation for fopen() and use a mode that allows you to write anywhere in the file.

    Jim

  3. #3
    Registered User
    Join Date
    Oct 2012
    Location
    Philippines
    Posts
    7
    so any suggestion?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Yes, study the documentation and pick a mode that lets you write anywhere in the file.

    Jim

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    You might want to read the documentation for fseek().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct field containing size of struct
    By DL1 in forum C Programming
    Replies: 7
    Last Post: 10-10-2011, 10:37 PM
  2. How i use a struct field on a function?
    By Rafa T. in forum C Programming
    Replies: 6
    Last Post: 12-05-2010, 10:32 AM
  3. Creating struct from existing data
    By Dest in forum C Programming
    Replies: 5
    Last Post: 04-13-2010, 06:58 PM
  4. Replies: 6
    Last Post: 12-02-2009, 08:47 AM
  5. Replies: 2
    Last Post: 06-07-2009, 08:49 AM