Existing field in struct to be overwritten. BINARY FILES.

This is a discussion on Existing field in struct to be overwritten. BINARY FILES. within the C Programming forums, part of the General Programming Boards category; Hi guys. I'm having trouble with overwriting a specific field in my struct. Anyone can help me? What should I ...

  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
    2,919
    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
    2,919
    Yes, study the documentation and pick a mode that lets you write anywhere in the file.

    Jim

  5. #5
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    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, 11: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, 07:58 PM
  4. Replies: 6
    Last Post: 12-02-2009, 08:47 AM
  5. Replies: 2
    Last Post: 06-07-2009, 09:49 AM

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