Skip to content

Instantly share code, notes, and snippets.

@AlexsJones
Created October 25, 2012 08:43
Show Gist options
  • Select an option

  • Save AlexsJones/3951479 to your computer and use it in GitHub Desktop.

Select an option

Save AlexsJones/3951479 to your computer and use it in GitHub Desktop.
char *getline(void)
{
char* accumulator = malloc(100);
char readBuf[100];
int accumulatorSize = 100;
*accumulator = '\0';
while (!feof(stdin))
{
fgets(readBuf, 99, stdin);
strcat(accumulator, readBuf);
/* possible fencepost error here */
if (readBuf[strlen(readBuf)] != '\n')
{
accumulatorSize += 100;
accumulator = realloc(accumulator, accumulatorSize);
/* should probably check for realloc returning null */
}
else
break;
}
return accumulator;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment