Skip to content

Instantly share code, notes, and snippets.

@jshardy
Created February 25, 2019 22:54
Show Gist options
  • Save jshardy/37f3654176d93fa8fc116b1616061b50 to your computer and use it in GitHub Desktop.
Save jshardy/37f3654176d93fa8fc116b1616061b50 to your computer and use it in GitHub Desktop.
GetLine()
char *GetLine()
{
size_t current_line_size = MAX_STR;
char *str_line = NULL;
char c = 0;
size_t index = 0;
str_line = malloc(sizeof(char) * current_line_size);
while((c = fgetc(stdin)) != '\n')
{
str_line[index++] = c;
if(index == current_line_size)
{
current_line_size += MAX_STR;
str_line = realloc(str_line, sizeof(char) * current_line_size);
}
}
str_line[index] = 0;
if(str_line[0] == 0)
{
free(str_line);
str_line = NULL;
}
return str_line;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment