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

  • Home
  • SEARCH
  • 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 6834317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:05:48+00:00 2026-05-26T23:05:48+00:00

I have the following struct: struct Records { int Number; char Name[20]; float Salary;

  • 0

I have the following struct:

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

Writing two records using:

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

After writing, Im having trouble reading from it.

FILE * read;
read = fopen("binaryfile.dat","rb");

for(int x =0;x<2;x++)
{
fread(&records.Number, sizeof(records.Number), 1, read);
fread(&records.Name, sizeof(records.Name), 1, read);
fread(&records.Salary, sizeof(records.Salary), 1, read);
printf("%d %s %f\n",records.Number,records.Name,records.Salary);
} 

The first line gets printed twice, and the float comes out as some weird number. I’ve double and tripled checked for the past 2 hours yet I cant find out what im doing wrong 🙁

  • 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-26T23:05:49+00:00Added an answer on May 26, 2026 at 11:05 pm

    You could write the whole struct in one go instead, makes it easier to read:

    fwrite(&MyRecords, sizeof(MyRecords), 1, binaryfile);
    

    besides that you also have wrong sizeof() it shouldn’t be sizeof(&MyRec..) it should be sizeof(MyRec..) without &.

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

Sidebar

Related Questions

I'm using C++, and I have the following structures: struct ArrayOfThese { int a;
I have the following struct: typedef struct{ int vin; char* make; char* model; int
VC++ 2010 Lets say I have the following: struct person { char * name;
I have the following struct in C++: #define MAXCHARS 15 typedef struct { char
I have the following construction: typedef struct bucket { char *key; ENTRY *data; struct
I have the following code: class outer { struct inner { int var1; int
I have the following use case, a struct with some boolean and int variables
I have the following struct john { int oldA; int A; } myJohn; DWORD
Suppose I have the following struct: struct sampleData { int x; int y; };
I have the following code: struct simple { simple (int a1, int a2) :

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.