Created
February 25, 2019 22:54
-
-
Save jshardy/37f3654176d93fa8fc116b1616061b50 to your computer and use it in GitHub Desktop.
GetLine()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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