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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:40:41+00:00 2026-06-18T08:40:41+00:00

What should be incredible simple has me stumped. All I am doing is writing

  • 0

What should be incredible simple has me stumped. All I am doing is writing a getLine method that should read a line into a buffer. I allocate memory to the buffer starting at 10 bytes, then fgets into a char array. The idea is that I attempt this over and over, doubling the size of the buffer, until it gets the whole line. However what I seem to be observing is that it reads a portion of the line, and then on the next attempt continues where it ran out of room on the last attempt, so it gives the last part of the line. Any insight it to what is wrong, I expect with my realloc memory to the buffer, would be much appreciated.

char * mygetline( char **buffer,  FILE * infile )
{
    int buffSiz = 10;
    *buffer = calloc( buffSiz, 1 );
    do
    {
        char * result = fgets(*buffer, sizeof *buffer ,infile);
        if(result == NULL)
        {
            free(buffer);
            return NULL;
        }

        if(strchr(*buffer, '\n'))
            return *buffer;
        else
        {
            char *newBuf;
            buffSiz = buffSiz*2;
            newBuf = realloc(NULL, buffSiz);
            char *buffer = newBuf;
            printf("%d", buffSiz);
            printf("%s", *buffer);
        }
    } while (1);  // INFINITE LOOP - WE ONLY GET OUT BY RETURNING FROM WITHIN
  • 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-18T08:40:42+00:00Added an answer on June 18, 2026 at 8:40 am

    That’s how the file functions works. You have to get the file position at the start, then reset the position every time you attempt a new read.

    You can get the current position with the ftell function, and then set the position with the fseek function.


    Instead of doing that (getting position and constant repositioning the file position), why not use the fact that realloc reallocates the passed in buffer and keeps the contents together with the fact that the read functions reads the next part? In that case you need to have a pointer to where to read next, and if no newline then just reallocate and advance the pointer?

    Something like this:

    char *readpos = *buffer;
    size_t readsize = bufSize;
    
    while (1)
    {
        char *p = fgets(readpos, readsize, infile);
        if (!p)
            break;  /* Error */
    
        /* Search from the end, as there's where the newline should be */
        if (strrchr(p, '\n') != NULL)
            return *buffer;  /* Found the newline */
    
        /* Need to allocate more memory */
        bufSize += bufsize;
        *buffer = realloc(*buffer, bufSize);
    
        /* Set the pointer to next position to read into */
        readpos = *buffer + strlen(buffer);
    
        /* Loop takes case of trying again */
    }
    

    Note: The above code was written in the browser, not tested, and might need some tuning to make it work properly.

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

Sidebar

Related Questions

Should be a simple question for SDL experts. I am confused about the following
Should I merge those tables into one table? Is there any tricky alternative? Table
Should I include/require_once the parent class that I am extending in PHP? for example
I'm currently employed at a small ASP.NET/MS-SQL shop. My team has noticed that a
Been scratching my head for a while with what should be a simple Update
Hi I am having an incredibly hard time with what should be a simple
Converting an existing Android application to a library is incredibly simple : All I
Such simple code, why isn't it working? When the page loads, it should display
$(#link1).click(function(){ $(#div1).load('../test.html'); return false; }); This is an incredibly simple .load() function that simply
This has been incredibly frustrating, especially given the simplicity of it. Simple javascript: <script

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.