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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:28:04+00:00 2026-05-31T11:28:04+00:00

Below is the structure defined. typedef struct{ int a; char b; }X; typedef struct{

  • 0

Below is the structure defined.

typedef struct{
    int a;
    char b;
}X;
typedef struct{
    X m;
    int c;
    char d;
}B;
B n,q;
n.m.a = 12;
n.m.b = 'a';
n.c = 13;
n.d = 'b';

I do a fwrite of the following structure in a file. File is opened as below.

fp = fopen("D://tests//t11test.txt","wb");
fwrite(&n, sizeof(B), 1, fp);

The fwrite is successful and I checked the contents of the file corresponding to fp.
But when I do a fread on the same file after closing and reopening the file, I am not able to read the contents of the sub-structure m. The fread is

fp = fopen("D://tests//t11test.txt","rb");
fread(&q, sizeof(B), 1,fp);

Where am I going 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-31T11:28:05+00:00Added an answer on May 31, 2026 at 11:28 am

    I can’t see what the problem is, but, FWIW, this worked:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    #include <assert.h>
    
    typedef struct{ int a; char b; }X;
    typedef struct{ X m; int c; char d; }B;
    
    void print_B(B* a_b)
    {
        printf("{ { %d, %c }, %d, %c }",
               a_b->m.a,
               a_b->m.b,
               a_b->c,
               a_b->d);
    }
    
    int main()
    {
        /* Write structure. */
        {
            FILE* fp;
            B n;
            n.m.a = 12;
            n.m.b = 'a';
            n.c = 13;
            n.d = 'b';
            fp = fopen("x.dat", "wb");
            assert(0 != fp);
            if (1 != fwrite(&n, sizeof(B), 1, fp))
            {
                fprintf(stderr, "Failed to fwrite(): %s\n", strerror(errno));
                return 1;
            }
            fclose(fp);
    
            printf("wrote: ");
            print_B(&n);
            printf("\n");
        }
    
        /* Read structure. */
        {
            FILE* fp;
            B q;
            fp = fopen("x.dat", "rb");
            assert(0 != fp);
            if (1 != fread(&q, sizeof(B), 1, fp))
            {
                fprintf(stderr, "Failed to fread(): %s\n", strerror(errno));
                return 1;
            }
            fclose(fp);
    
            printf("read : ");
            print_B(&q);
            printf("\n");
        }
    
        return 0;
    }
    

    Output:

    wrote: { { 12, a }, 13, b }
    read : { { 12, a }, 13, b }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following structure and object of structure defined in the header file
I am writing the below structure in to the file using Binary Writer. File
I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
I have a data structure which is as given below: class File { public
I have seen many programs consisting of structures like the one below typedef struct
I had a need to declare a union inside a structure as defined below:
Let's say I have a class structure that is defined below: Class Item(models.Model): ...
It's possible to declare variables with the below structure in C++ private: public: protected:
I have a Zend Framework application structure as below: /application /library /Zend /Core /Filter
I am trying to convert an array of the RECT structure (given below) into

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.