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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:22:23+00:00 2026-05-31T09:22:23+00:00

Trying to write a function that will write my structure array to a binary

  • 0

Trying to write a function that will write my structure array to a binary file.
I thought I was able to copy it over as a complete unit one array at a time but it’s not working for me.

Do I need to write each individual sub-entity or is there a way to do it in a large clump?

    {

       void export_binary(char *data_base_name, student_record *ptr,int array_flag,unsigned int rec_cnt)
    {
       if (array_flag==-99)
            {
                printf("\n\nDatabase not loaded...\n\nPlease IMPORT or CREATE a new database.\n\n");
                system("pause");
                return;
            }

    int rec_counter;
    FILE *pf;

        pf=fopen(data_base_name,"wb");

        if (!pf)
            {
                printf("*** FILE OPENING ERROR ***\n\n");
                system("pause");
                return ;
            }
        for ( rec_counter=0; rec_counter <= rec_cnt; rec_counter++)
            {
                fwrite(&ptr[rec_counter], sizeof(student_record), 1, pf);
            }


                if ((fclose(pf))!=0)
            {
                printf("\n\n*** FILE Error - Closing file FAILED! ***\n\n");
                system("pause");
                return;
            }
                printf("\n\n*** Database SAVED ***");
                system("pause");
                return;
}
  • 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-31T09:22:25+00:00Added an answer on May 31, 2026 at 9:22 am

    You’re not actually stating what the error is (this is vitally important when you’re trying to ask for help) but, provided you don’t have anything like pointers in your structures, you can do it in all at once by replacing:

    for ( rec_counter=0; rec_counter <= rec_cnt; rec_counter++)
    {
        fwrite(&ptr[rec_counter], sizeof(student_record), 1, pf);
    }
    

    with:

    int actual = fwrite (ptr, sizeof(student_record), rec_cnt, pf);
    if (actual != rec_cnt) ... a problem occurred.
    

    (you should always check the return value).

    So no, you don’t have to write each element of each structure in the array separately, you can go the other way and write the whole array in one hit.


    As to your additional info:

    I get a file that has the element of my records in it but garbage for everything else.

    Yes, you will, if you write out more records than you have populated. In other words, if rec_cnt is the size of the array rather than the number of array elements you have populated, you’ll have junk at the end.

    I was assuming (perhaps wrongly) that rec_cnt was the number of elements populated. You should have a variable that has that value somewhere (if rec_cnt isn’t it) since otherwise you don’t know which elements are good and which are rubbish.

    Use that variable to decide how many elements to write with fwrite.


    If it’s not the later array elements that are rubbish, but rather the unused space within each element, that’s expected and irrelevant. Structures are allowed to have padding within them (and at the end) for alignment purposes.

    Provided the padding is identical when writing and reading, you can safely ignore it.

    If the alignment may be different the, yes, you’ll probably have to either:

    • turn off padding for the structure (implementation-defined support such as #pragma pack); or
    • write out the fields of the structure separately to remove the padding.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function that will traverse the file directory and give
I am trying to write a function that will pull the name of a
i am trying to write a function that will make DataRow[column] return nullable typed
I'm trying to write a function that will let me red-shift or blue-shift a
I am trying to write a function that will filter a list of tuples
I am trying to write a function that will: Disable the submit button when
I am trying to write a function that will randomly return an (x,y) co-ordinates
I'm trying to write a function that will return all the available cars for
I am trying to write a function that will convert a generic list to
I'm trying to write a regex function that will identify and replace a single

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.