There is no ready-made function to read everyting from stdin, but creating your own is fortunately easy. Untested code snippet, with some explanation in comments, which can read arbitrarily large amount of chars from stdin:
size_t size = 0; // how many chars have actually been read size_t reserved = 10; // how much space is allocated char *buf = malloc(reserved); int ch; if (buf == NULL) exit(1); // out of memory // read one char at a time from stdin, until EOF. // let stdio to handle input buffering while ( (ch = getchar()) != EOF) { buf[size] = (char)ch; ++size; // make buffer larger if needed, must have room for '\0' below! // size is always doubled, // so reallocation is going to happen limited number of times if (size == reserved) { reserved *= 2; buf = realloc(buf, reserved); if (buf == NULL) exit(1); // out of memory } } // add terminating NUL character to end the string, // maybe useless with binary data but won't hurt there either buf[size] = 0; // now buf contains size chars, everything from stdin until eof, // optionally shrink the allocation to contain just input and '\0' buf = realloc(buf, size+1);
fgetsis fine that way, it'll give you a line at a time. Alternative would be to read a char at a time, withgetcharor something. In either case, if you want to read arbitrarily long input, you'll need to read it in parts and do your own buffer handling.fgets()in a loop, and you'll probably need to allocate memory to hold the growing string.