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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:50:40+00:00 2026-06-15T16:50:40+00:00

I am trying to read in a struct from a file and then display

  • 0

I am trying to read in a struct from a file and then display (and sort) and array. I am having trouble though which I think is to do with me not accessing the correct memory. When I print the array it comes up as loads of random numbers.

struct details
{
    int numberOfPresents;
    int numberOfBuildings;
    int buildings[];
};

void print_int_array(const int *array) 
{ 
    for(int i=0; i<200; i++) 
        printf("%d | ", array[i]);

    putchar('\n');
} 

void sort(int buildings[], int count)
{
    int i, j, temp;
    do {
        j = 0;  
        for (i = 0;i<count-1;i++)
        {
            if (buildings[i] < buildings[i+1])
            {
                j = 1;
                temp = buildings[i];
                buildings[i] = buildings[i+1];
                buildings[i+1] = temp;
            }
        }
    } while (j == 1);
}

int main()
{
    FILE *fp;
    fp = fopen("buildings.out", "r");   
    struct details data1;
    size_t structSize = sizeof(struct details);
    //size_t arraySize = sizeof(int)*sizeof(buildings);
    fread(&data1, structSize, 1, fp);
    for(int i=0; i<200; i++) 
        printf("%d | ", data1.buildings[i]);

    //sort(data1.buildings );
    //print_int_array(data1.buildings, arraySize);
    //printf("Number of Houses: %d\n",numberOfHouses(data1.numberOfPresents, data1.buildings));
    fclose(fp);
    return 0;
}
  • 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-15T16:50:41+00:00Added an answer on June 15, 2026 at 4:50 pm

    You’ve got two issues working against you printing the data.

    • Enough room is not allocated for the records.
    • Enough data is not read for the records.

    The line struct details data1 only allocates enough room on the stack for one copy of the struct. You need enough for 200 of them. I’d immediately suggest an array.

    struct details data1[200];
    

    When you perform the read, fread(&data1, structSize, 1, fp), you’re only reading in one record of size structSize. Now that you have enough memory allocated to read in 200 records, you can bump up the number of records you’re reading to 200 as well.

    fread(data1, structSize, 200, fp);
    

    (Notice we dropped the & because we’re dealing with an array now. Arrays automatically return their base address if you just reference them by name.)

    Now, what if your file doesn’t have 200 records in it? You probably need to capture the return value of fread() to determine how many records you actually read.

    int intNumberOfRecords = fread(&data1, structSize, 200, fp);
    for(int i=0; i<intNumberOfRecords ; i++)
      [...]
    

    Now that we have that working, we can look a little closer at the srtuct itself. We’ve got a challenge with the definition that we can’t easily overcome.

    struct details{
        int numberOfPresents;
        int numberOfBuildings;
        int buildings[];
    };
    

    The last member, buildings[], is not going to read correctly from a file. This is because it’s only a 32-bit integer at best in a 32-bit memory model. In other words, what you’ll be reading from the disk is just a 32-bit number that points to somewhere in memory. What you won’t end up with is an array that contains the buildings. If you try to access it (i.e. in your sort routine) you’ll more than likely seg-fault and your program will never work. Trying to post a general solution for this is a little out of the scope of my answer. Suffice it to say, you’ll either have to go with a fixed size array or dynamically write variable size arrays to disk (variable length records). The fixed size array would be a lot easier. If we change your definition to the following, we’d load up some data from the disk.

    struct details{
        int numberOfPresents;
        int numberOfBuildings;
        int buildings[16];
    };
    

    We’d also avoid seg-faulting, which is a nice plus. However, I don’t know what your input file looks like so I don’t know if this will work given your data.

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

Sidebar

Related Questions

I'm trying to read encrypted struct data from file using fread(). once i get
I am trying to read some XML code from a website, and am having
I am trying to read from a file and encrypt the data using AES
I'm trying to read in a list of files from another file. The file
I am currently trying to read in names from an input file. The file
I'm trying to read a binary file into a C# struct. The file was
I'm trying to read data values from a text file and store them in
I am trying to read Data from a Text file & storing it inside
Trying to read headers for a csv file with: reader = csv.DictReader(open(PATH_FILE),skipinitialspace=True) headers =
Im trying to read a column of String values from my DB. Im trying

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.