Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8944905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:07:37+00:00 2026-06-15T12:07:37+00:00

I am tyring to use stucts with binary files in C. I read in

  • 0

I am tyring to use stucts with binary files in C. I read in a file of structs of people. Each person has an attribute is_a_winner, is_runner_up if is_a_winner is true I should add Gold in the Prize field, if is_runner_up is true, add Silver otherwise add bronze. It works on some structs but not others. I think it may be to do with the positioning of the ‘cursor’ in the file.

struct prizelist{
    char name[10];
    int is_a_winner;
    int is_runner_up;
    int age;
    char prize[20];
};

void addPrize() {
    FILE *fp;
    if ((fp = fopen("prize.out", "rb+"))!=NULL) {
        size_t itemSize = sizeof(struct prizelist);
        struct prizelist item;
        fread(&item,1,itemSize, fp);
        while (!feof(fp)) {
            if (item.is_a_winner) {
                strcpy( item.prize, "Gold" );   
            } else if (item.is_runner_up) { 
                strcpy( item.prize, "Silver" );         
            } else {
                strcpy( item.prize, "Bronze" );             
            }
            fseek(fp, itemSize, SEEK_CUR);
            fwrite(&item, itemSize, 1, fp);
            fread(&item,1,itemSize, fp);
        }
        fclose(fp);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:07:39+00:00Added an answer on June 15, 2026 at 12:07 pm

    Your code as presented can be boiled down to:

    fread(&item,1,itemSize, fp);
    while (!finished) {
      fseek(fp, itemSize, SEEK_CUR);
      fwrite(&item, itemSize, 1, fp);
      fread(&item,1,itemSize, fp);
    

    The problem is in your fseek() call; you are doing a relative seek from your current (SEEK_CUR) position forward in the file and then writing to it… if I understand your intention to be to update the record you just read, you’ve seeked in the wrong direction. If your first record was record 0, your position after the read is at record 1, then you seek to record 2, and then you write. Whoops! Instead, seek with: fseek(fp, -itemSize, SEEK_CUR); to move backwards.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
I'm beginning to learn about the use of structs in C. It's challenging and
I'm trying to use an unordered_set to maintain an unique list of structs. I've
I'm trying to copy some nested structs to device memory for kernel use in
I'm trying to use vectors inside structs with LLVM. I have the following C
I'm trying to use tpl to serialize structs that contain wchar_t* strings. The code
I'm trying to make use of typedef structs inside of my C++ program. I
I'm trying to make an array of structs where each struct represents a celestial
I'm having problems trying to use an array of structs which doesn't have an
i'm trying use facebook API to upload photo in my fan page. I downloaded

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.