Skip to content

Instantly share code, notes, and snippets.

@insipx
Created February 25, 2016 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save insipx/49e0c8848ad6cbcc1981 to your computer and use it in GitHub Desktop.
Save insipx/49e0c8848ad6cbcc1981 to your computer and use it in GitHub Desktop.
char * readUUI()
{
//allocate 64bytes for usage
size_t size = 64;
char *str = (char*)malloc(size);
if(str){
}else{
die("Mem allocate unsuccessful");
}
char c;
int i = 0;
do
{
c = fgetc(stdin);
str[i] = c;
i++;
//reallocate memory by +1 every time, we use very little memory this way
//this isn't the most mem-efficient method but it should use less cycles
if(i >= size){
size++;
str = (char*) realloc(str, size);
}
if(c == '\n' || c == EOF){
str[i] = '\0';
break;
}
}while(1);
printf("%s%s", " This is the Readline String: ", str);
printf("%s%d%s", " and this is the size ", sizeof(str)/sizeof(char), "\n");
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment