Skip to content

Instantly share code, notes, and snippets.

@darkodemic
Forked from btmills/ufgets.c
Last active August 29, 2015 14:23
Show Gist options
  • Save darkodemic/1a41d47ccf25b2b6281f to your computer and use it in GitHub Desktop.
Save darkodemic/1a41d47ccf25b2b6281f to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
/*
* Similar to fgets(), but handles automatic reallocation of the buffer.
* Only parameter is the input stream.
* Return value is a string. Don't forget to free it.
*/
char* ufgets(FILE* stream)
{
unsigned int maxlen = 128, size = 128;
char* buffer = (char*)malloc(maxlen);
if(buffer != NULL) /* NULL if malloc() fails */
{
int ch = EOF;
int pos = 0;
/* Read input one character at a time, resizing the buffer as necessary */
while((ch = fgetc(stream)) != '\n' && ch != EOF && !feof(stream))
{
buffer[pos++] = ch;
if(pos == size) /* Next character to be inserted needs more memory */
{
size = pos + maxlen;
buffer = (char*)realloc(buffer, size);
}
}
buffer[pos] = '\0'; /* Null-terminate the completed string */
}
return buffer;
}
#include <stdio.h>
/*
* Similar to fgets(), but handles automatic reallocation of the buffer.
* Only parameter is the input stream.
* Return value is a string. Don't forget to free it.
*/
char* ufgets(FILE* stream);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment