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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:44:31+00:00 2026-05-29T07:44:31+00:00

I tried to make a program that reads ELEM structures from keyboard and writes

  • 0

I tried to make a program that reads ELEM structures from keyboard and writes them to a file (file.bin) and then writes unique items to another file (aux.bin). The problem is that it can not close opened files in void function(), can you please tell me what I did wrong?

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

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

void clear_stdin()
{
    char str[255];
    fgets(str,255,stdin);
}

int create()
{
    FILE *f;
    int d=0;
    int c;
    int n=0;
    ELEM s;
    f=fopen("file.bin","wb");
    if(f==NULL)
    {
        printf("create(): Could not open file.bin for read\n");
        return;
    }
    do{
    printf("Add elements to file?:\n1 - yes\n2 - no\n");
    scanf("%d",&c);
    if (c==1)
    {
        printf("Name=");
        clear_stdin();
        fgets(s.name,80,stdin);
        printf("P=");
        scanf("%d",&s.p);
        fwrite(&s,sizeof(ELEM),1,f);
        n++;
    }
    else
        d=1;
    } while(d==0);
    fclose(f);
    return n;
}

void show(int n)
{
    FILE *f;
    ELEM s;
    int i=0;
    if(n==0)
        return;
    f=fopen("file.bin","rb");
    while(i<n)
    {
        fread(&s,sizeof(ELEM),1,f);
        puts(s.name);
        printf("\t%d\n",s.p);
        i++;
    }
    fclose(f);
}

int add(int n)
{
    FILE *f;
    int d=0;
    int c;
    ELEM s;
    f=fopen("file.bin","ab");
    if(f==NULL)
    {
        printf("add(): Could not open file.bin for append\n");
        return;
    }
    do{
    printf("Add elements to file?:\n1 - yes\n2 - no\n");
    scanf("%d",&c);
    if (c==1)
    {
        printf("Name=");
        clear_stdin();
        fgets(s.name,80,stdin);
        printf("P=");
        scanf("%d",&s.p);
        fwrite(&s,sizeof(ELEM),1,f);
        n++;
    }
    else
        d=1;
    } while(d==0);
    fclose(f);
    return n;
}


void func(int n)
{
    FILE *f,*g;
    ELEM v[20],w;
    int i=0,j,k,x=0,s,gn=0,test;
    f=fopen("file.bin","rb");
    g=fopen("aux.bin","wb");
    if((g==NULL)||(f==NULL))
    {
        if(g==NULL)
            printf("function() : Could not open aux.bin for write\n");
        if(f==NULL)
            printf("function() : Could not open file.bin for read\n");
        return;
    }
    i=0;
    while(i<n)
    {
        fread(&v[i],sizeof(ELEM),1,f);

        i++;
    }
    for(j=0;j<n;j++)
    {
        for(k=j+1;k<n;k++)
        {
            if(v[j].p==v[k].p)
                x=1;

        }
        if(x==0)
        {
            s=strcmp(v[j].name,v[k].name);
            if(s!=0)
            {
                fwrite(&v[j],sizeof(ELEM),1,g);
                fread(&w,sizeof(ELEM),1,g);

                gn++;
            }
        }
        x=0;
    }
    test=fclose(g);
    if(test!=0)
        printf("function() : failed to closed file g\n");
    test=fclose(f);
    if(test!=0)
        printf("function() : failed to closed file f\n");
    g=fopen("aux.bin","rb");
    if(g==NULL)
    {
        printf("function() : Could not open aux.bin for read\n");
        return;
    }
    if(gn==0)
        return;
    i=0;
    while(i<gn)
    {
        fread(&w,sizeof(ELEM),1,g);
        puts(w.name);
        printf("\t%d\n",w.p);
        i++;
    }
    fclose(g);
}

int main()
{
    int k=0,r,n;
    do{
        printf("1 - create file\n2 - add elements to file\n3 - show elements\n4 - put unique elements in another file\n5 - exit program\n");
        scanf("%d",&r);
        switch(r)
        {
            case 1 : n=create(); break;
            case 2 : n=add(n); break;
            case 3 : show(n); break;
            case 4 : func(n); break;
            case 5 : k=1; break;
            default : printf("Command unrecognized!\n");
        }
    } while(k==0);
    return 0;
}

EDIT:
I edited the code

aux.bin was never created, that’s why it freezes at fread(&w,sizeof(ELEM),1,g);
But why it can’t create aux.bin ?
I don’t get any error messages but when I open the project folder, there’s just file.bin.


EDIT:
I switched form MinGW and Visual Studio to gcc, and now it creates both files, everything works excepting fflush(stdin) . I replaced gets(s.name); to fgets(s.name,80,stdin); .


EDIT:
I replaced fflush(stdin) with a function that clears the stdin and now it works flawlessly with gcc in linux.
In windows it doesn’t work.

  • 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-29T07:44:32+00:00Added an answer on May 29, 2026 at 7:44 am

    You seem to be expecting your file handle pointers to become NULL when you close the file, that’s not true.

    Note that the fclose() function takes the pointer (an argument of type FILE *), so all it gets is the pointer’s value, it cannot change the caller’s pointer. For that to be possible, you would have to call it with a FILE **, i.e. a pointer to a pointer.

    It’s your responsibility to set the pointer to NULL after the fclose() call, if (and only if) fclose() said it succeeded, by returning 0. See the manual page for details.

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

Sidebar

Related Questions

I tried to make a program that writes struct elements to binary file and
I make several calls to a function that reads data from an input file.
While creating a file synchronization program in C# I tried to make a method
I've got a GUI C++ program that takes a shell command from the user,
I am supposed to write a program that should read from input numbers in
I was trying to make a simple bubblesort program. Read in numbers from a
I'm trying to make a little java program that executes a system command but
Tried to make a little old school ajax (iframe-javascript) script. A bit of mootools
I tried to make the title as clear as possible... here is my scenario:
I tried to make an independent copy of an array but couldnt get one.

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.