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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:12:51+00:00 2026-05-28T18:12:51+00:00

I’m trying to make a program that creates and reads a binary file, which

  • 0

I’m trying to make a program that creates and reads a binary file, which contains “struct elements”; can you please tell me what I did wrong?
I got errors telling me that “s” is not a pointer in function fread()… so I declared ELEM *s; instead of ELEM s;

#include <stdio.h>
#include <stdlib.h>

typedef struct element{
    char name[80];
    int p;
}ELEM;

void create()
{
    FILE *f;
    int d=0;
    char c;
    ELEM *s;
    f=fopen("file.bin","wb");
    do{
    printf("Add elements to file?: (y/n)");
    fflush(stdin);
    scanf("%c",&c);
    if (c=='y')
    {
        printf("Name=");
        gets((*s).name);
        printf("P=");
        scanf("%d",(*s).p);
        fwrite(s,sizeof(ELEM),1,f);
    }
    } while(d==0);
    fclose(f);
}

void show()
{
    FILE *f;
    ELEM *s;
    f=fopen("file.bin","rb");
    while(feof(f)!=NULL)
    {
        fread(s,sizeof(ELEM),1,f);
        puts((*s).name);
        printf("\t%d\n",(*s).p);
    }
    fclose(f);
}

void add()
{
    FILE *f;
    int d=0;
    char c;
    ELEM *s;
    f=fopen("file.bin","ab");
    do{
    printf("Add elements to file?: (y/n)");
    fflush(stdin);
    scanf("%c",&c);
    if (c=='y')
    {
        printf("Name=");
        gets((*s).name);
        printf("P=");
        scanf("%d",(*s).p);
        fwrite(s,sizeof(ELEM),1,f);
    }
    } while(d==0);
    fclose(f);
}


/*void function()
{

}*/

int main()
{
    int k=0,r;
    do{
        printf("1 - create file\n2 - add elements to fil\n3 - show elements\n4 - put unique elements in another file\n5 - exit program\n");
        scanf("%d",&r);
        switch(r)
        {
            case 1 : create(); break;
            case 2 : add(); break;
            case 3 : show(); break;
            case 4 : printf("Function not defined!\n"); break;
            case 5 : k=1; break;
            default : printf("Command unrecognized!\n");
        }
    } while(k==0);
    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-05-28T18:12:52+00:00Added an answer on May 28, 2026 at 6:12 pm

    The first parameter passed to fwrite should be an address, and the variable whose address you pass must have enough memory to hold the number of objects you plan to read.

    So there are two ways:

    Creating Variable on Stack:

    You allocate the variable on stack and pass its address to fwrite

    ELEM s;
    fwrite(&s,sizeof(ELEM),1,f);
    

    or

    Dynamic Memory allocation:

    ELEM *s;   
    

    Should be allocated an memory equivalent to hold no of objects of type ELEM that you want to read.

    ELEM *s = malloc(sizeof *s);
    

    In this case remember to free the memory once done with your use:

    free(s);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small

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.