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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:27:05+00:00 2026-06-01T04:27:05+00:00

I have a structure typedef struct store { char name[11]; int age; } store;

  • 0

I have a structure

typedef struct store
{
   char name[11];
    int age;
} store;

and a main function(below is part of it):

int main()
{
    int i=0;
    int inputs;
    char line[100];
    char name[11];
    char command[11];
    store read[3000];

    while(i < 3000 && gets(line) != NULL)  
    {
        int tempage;
        inputs = sscanf(line, "%10s %10s %d", command, name, &tempage);
        if (inputs == 3)
        {
            if (strcmp(command, "register") == 0)
             {
             strncpy(read[i].name, name,10);
             read[i].age = tempage;
             i++;
             ....

I need to modify it so that it can read a line of arbitrary length, and store the name from the line which is also a string of arbitrary length using malloc and realloc.

How should I approach this?

  • 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-01T04:27:06+00:00Added an answer on June 1, 2026 at 4:27 am

    What you need to do is read the line in smaller increments, and resize your buffer as you go.

    As an example (not tested and not meaning to be particularly elegant, just an example):

    char *readline(FILE *f)
    {
       char *buf = NULL;
       size_t bufsz = 0, len = 0;
       int keep_going = 1;
    
       while (keep_going)
       {
          int c = fgetc(f);
          if (c == EOF || c == '\n')
          {
             c = 0;             // we'll add zero terminator
             keep_going = 0;    // and terminate the loop afterwards
          }
    
          if (bufsz == len)
          {
             // time to resize the buffer.
             //
             void *newbuf = NULL;
             if (!buf)
             {
                bufsz = 512; // some arbitrary starting size.
                newbuf = malloc(bufsz);
             }
             else
             {
                bufsz *= 2; // issue - ideally you'd check for overflow here.
                newbuf = realloc(buf, bufsz);
             }
    
             if (!newbuf)
             {
                // Allocation failure.  Free old buffer (if any) and bail.
                //
                free(buf);
                buf = NULL;
                break;
             }
    
             buf = newbuf;
          }
    
          buf[len++] = c;
       }
    
       return buf;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the structure typedef struct EData { int a; char c; } Edata
I have the following structure typedef struct DeviceInfo { char[30] name; char[30] serial Number;
If I have a structure such as typedef struct _people { char *name; bool
Let's say I have a first structure like this: typedef struct { int ivalue;
I have a structure typedef struct myStruct_st { int a; }myStruct; It can be
I have a structure such as typedef struct FT_Bitmap_ { int rows; int width;
I have this structure: typedef struct { int data[10]; } small_structure; and this code:
I have the following for my HashTable structure: typedef char *HashKey; typedef int HashValue;
I have a structure like this typedef struct{ int stat; }dot; And I would
I have a simple structure that is defined like so: typedef struct { int

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.