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

The Archive Base Latest Questions

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

Working on a problem where I have to read data from a file into

  • 0

Working on a problem where I have to read data from a file into a struct.

The file is organized so that there is a name, a few lines of ASCII art terminated by a # and a rating. Here is an example

Sample Name
( S )
( S )
# 5

I have my struct set up like this:

typedef struct
 {
   char* name;
   char* art;
   int rating;
 }CASE;

My problem is that I keep getting a segmentation fault when I try to dynamically allocate memory for the name string in the code below:

/*FPin is file pointer to the txt file and all is the array of structs*/
void readFile(FILE* FPin, CASE** all)
{ 
  CASE* walker = *all;
  int count = 0;
  char buffer[160];
  char* bufferPtr = buffer;
  char nameBuffer[100];

  /*Reads in the name*/

  while(fscanf(FPin, "%[^\n]", nameBuffer))
  {
    printf("string is %s\n", nameBuffer);
    walker->name = (char*)malloc(sizeof(char)*(strlen(nameBuffer+1)));  /*ERROR*/  
    strcpy(walker->name, nameBuffer);
  } 

  return;
  }

I made a note of where I believe the error to be in the code above because once I added that line, I started getting it.

I’m basically reading a name from a text into a nameBuffer (array) and then using strcpy to copy this name into the struct. Any advice on how to fix this?

Thanks for looking.

I’ll include the rest of my source below:

int main (void)
{
  CASE* all;
  FILE* FPin;

  if((FPin = fopen("art.txt", "r")) == NULL)
  {
    printf("Error opening file.");
    exit(100);  
  }

  allocateStructMem(&all);
  readFile(FPin, &all);

  fclose(FPin);
  return 0;
}

void allocateStructMem (CASE** all)
{
  if((*all = (CASE*)malloc(sizeof(CASE)*1000)) == NULL)
    {
      printf("Fatal memory error!\n");
      exit(1);
   }

  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-06-02T20:29:02+00:00Added an answer on June 2, 2026 at 8:29 pm

    strlen(nameBuffer+1)

    strlen(nameBuffer)+1
    

    You also have to do something like this when you malloc all:

    int allocateStructMem(CASE **all)
    {
        /*  +----- Note. And no need to cast as malloc returns (void *)
            |                                           */
        if((*all = malloc(sizeof(CASE*) * 1000)) == NULL)
    

    To prevent overflow you have to limit the length of fscanf, ie:

    while (fscanf(FPin, "%99[^\n]", nameBuffer) == 1) {
    

    The 1 ensure you have actually read something into nameBuffer.

    Also strcpy does not pad – but you might know that.

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

Sidebar

Related Questions

Working on a problem where I have to read data from a file into
I have a problem that I had working in a test but now in
I have some code that allows users to upload file attachments into a varbinary(max)
The app I am working on needs to read a JSON file that may
Is there a way to read a file's data but continue reading the data
I am trying to read in data from a text file (the time). and
I am currently working on Problem 62 I have tried the following code to
I working on project and have problem with threading and update of UI. I
I have a strange problem working with HTML,CSS in different browsers: Firefox 3.6 and
I'm working with oscommerce and i have problem with Paypal ipn When someone makes

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.