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 6863179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:45:04+00:00 2026-05-27T02:45:04+00:00

Any idea why the following code isn’t working? My struct: struct Records { int

  • 0

Any idea why the following code isn’t working?

My struct:

struct Records
{
    int Number;
    char Name[20];
    float Salary;
};

Code in main:

FILE * binaryfile;
binaryfile = fopen("binaryfile.dat","r+b");

struct Records MyRecords;

printf("Name: \n");
gets(MyRecords.Name);

printf("Yearly Salary: \n");
scanf("%f%*c",&MyRecords.Salary);

fseek(binaryfile,2L*sizeof(struct Records),SEEK_SET);

fread(&MyRecords.Number, sizeof(MyRecords.Number), 1, binaryfile);
fwrite(&MyRecords.Name, sizeof(MyRecords.Name), 1, binaryfile);
fwrite(&MyRecords.Salary, sizeof(MyRecords.Salary), 1, binaryfile);

I missed the lesson for binary so my understanding of this is very shaky, but this is what i’m trying to do:

With fseek, I’m going to the beginning of the 3rd record, which is “3,Random Name,50.00”.

Then I’m reading the 3 into MyRecords.Number (i’ve tested up to this point). Then when the pointer is on name, I’m attempting to overwrite it and the salary with new ones.

Yet somehow it doesn’t work. I’ve tried changing the file Opening to stuff like ab+, wb+, rb+, rb, with no success.

  • 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-05-27T02:45:04+00:00Added an answer on May 27, 2026 at 2:45 am

    IMHO, you always should use a complete structure in your code: read it (i.e. fread(&MyRecords, sizeof(MyRecord)…) ), change the elements, fseek again at the same place and fwrite the whole structure. The way you are doing, you may experience any sort of problems with data alignment.

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

Sidebar

Related Questions

any idea why the following code isn't working? Nothing happens when it's called, the
Any idea why the following code is not working mysql credentials are correct, have
Any idea why the following code doesn't print the amount of characters in the
Do you have any idea, why the following code: public class A { public
One of the following pieces of code generates a memory leak, any idea which
The following key event is not working. Any idea? class Frame(wx.Frame): def __init__(self): wx.Frame.__init__(self,
Any idea why the following mocking code does not work? org.hibernate.SessionFactory sessionFactory = Mockito.mock(SessionFactory.class);
any idea how if the following is possible in PHP as a single line
Anyone have any idea why CGContextFillPath won't work in the code snippet below? I'm
The following code should display table with borders around cells, but it doesn’t. Any

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.