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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:29:24+00:00 2026-06-02T04:29:24+00:00

I have create a struct and it has its id number, its value, and

  • 0

I have create a struct and it has its id number, its value, and its status.
I have a file that consist of the data(1 199 0 2 199 1…) 1 its the number, 199 is the value, 0 is the status and keep going like this…
I have used 1 function called filldata() to read 3 numbers at a time, which are for example, 1 199 0 and then put it into the passed element of a struct array.
And then, i used another function to call the this function to fill up the struct array.
The fillAll function will return the set of data tha.t had been copied from the file to the struct array
But i received a segmentation fault. Any idea why?
The codes explain better:

int filldata(struct Data_point *a, const char *filelocation)  
    {

        FILE *f;
        if((f=fopen(filelocation,"r"))==NULL)
            printf("You cannot open");

        if( fscanf(f, "%ld%lf%d", &(a->sampleNumber), &(a->value), &(a->status)) == 3)
            return 1;   
        else
            return 0;
    }

    int fillAll(struct Data_point *a, const char *filelocation)// I will pass the struct array and the location of my file string
    {
        int index=0;
        while(filldata(&a[index], filelocation))
            index++;

        return index;
    }
  • 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-02T04:29:25+00:00Added an answer on June 2, 2026 at 4:29 am

    you repeatedly open filename filelocation but never close the file handle f. You would keep reading the first line over and over again and eventually run out of filehandles.

    You can change filldata to take the file pointer check the snippet below i have added
    some additional checks , you also need to check the size of Data_point *a is within the
    allocated range as you fill it up

    int filldata(struct Data_point *a, File *f) 
    
    
        if( fscanf(f, "%ld%lf%d", &(a->sampleNumber), &(a->value), &(a->status)) == 3)
            return 1;   
        else
            return 0;
    }
    
    int fillAll(struct Data_point *a, const int data_point_size,const char *filelocation)// I will pass the struct array and the location of my file string
    {
    
        FILE *f;
        if((f=fopen(filelocation,"r"))==NULL) {
            printf("You cannot open");
           return 0;
        }
    
    
        int index=0;
        while(index < data_point_size &&  filldata(&a[index]))  {
            index++;
        } 
        fclose(f);
        return (index != data_point_size);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have create my own NSOpenGLView class, right now the data that i want
I am trying to create a struct that has multiple string arrays inside of
i have create a form (so it's PHP and HTML hybrid-code). it has ability
I have create a custom contextMenu using AS3 and can apply that to the
I was wondering if it was possible to create a struct containing a number
I have a struct struct request { int code; char *message; }; that I'd
I have a counter that counts the number of viewers in a chat room
I have been trying to create a red black tree that only implements an
I understand how to create a struct that can be used to access an
In my C# application, I have a large struct (176 bytes) that is passed

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.