Skip to content

Instantly share code, notes, and snippets.

@echiesse
Created December 23, 2025 19:38
Show Gist options
  • Select an option

  • Save echiesse/4a941fac42280c08ed5b231e9ce0076a to your computer and use it in GitHub Desktop.

Select an option

Save echiesse/4a941fac42280c08ed5b231e9ce0076a to your computer and use it in GitHub Desktop.
Reads data from stdin until EOF. Saves data to a memory buffer.
void* readStdinToBuffer(size_t* readSize)
{
const size_t INITIAL_SIZE = 10;
size_t size = INITIAL_SIZE;
char* buffer = malloc(size);
size_t i = 0;
while (1) {
if (i >= size) {
size *= 2;
buffer = realloc(buffer, size);
}
buffer[i] = fgetc(stdin);
if (feof(stdin)){
buffer = realloc(buffer, i);
*readSize = i;
break;
}
++i;
}
return buffer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment