>> Is there a better method for accepting keyboard input of unknown and potentially large size?
you can use a static buffer to read input:
Code:
#include <stdio.h>
#include <stdlib.h>
/*
function reads a line from 'stream' and
returns a pointer to a static buffer.
if 'stream' is NULL, returns contents of
last read operation.
if buffer can't grow any further,
a partial line is returned.
*/
const char * line(FILE * stream)
{
static char * ig_buffer = NULL;
static int ig_buffer_size = 0;
const int block_size = 32;
char * temp;
int ch, read = 0;
if(NULL == stream) {
return ig_buffer;
}
if(ig_buffer = NULL) {
ig_buffer = malloc(1);
ig_buffer[0] = 0;
}
if(feof(stream)) {
return NULL;
}
while(EOF != (ch = fgetc(stream)) && ch != '\n') {
if(read == ig_buffer_size) {
temp = realloc(ig_buffer, ig_buffer_size + block_size + 1);
if(NULL == temp) {
break;
}
ig_buffer = temp;
ig_buffer_size += block_size;
}
ig_buffer[read++] = ch;
}
ig_buffer[read] = 0;
return ig_buffer;
}
int main(int, char ** argv)
{
while(*(++argv)) {
FILE * fptr = fopen(*argv, "r");
if(fptr) {
while(line(fptr)) {
puts(line(NULL));
}
fclose(fptr);
}
}
return 0;
}