Originally Posted by
jimtuv
from Wikipidia: functional decomposition .... I love Google search!
Yeah, I just read that one too.
To take all of what is put in stding woudn't that take a variable length array?? Or I could just save the good and just chuck the leftover would be the my first thought.
You mean input a string of any length safely? I posted this here earlier in the week, it's the "general idea" behind my preferred method of input:
Code:
#define BSZ 1024
char *getline(FILE *st) {
char buf[BSZ],
*r;
int len, total = 0;
fgets(buf,BSZ,st);
len = strlen(buf);
r = malloc(len+1); // may want to error check this
strcpy(r,buf);
/* deal with absurdly long lines */
while ((len == BSZ-1)) {
total += len;
if (!fgets(buf,BSZ,st)) break;
len = strlen(buf);
r = realloc(r,total+len+1);
strcat(r,buf);
}
return r;
}
So you could use this:
Code:
char *input = getline(stdin);
[.....]
free(input);
BSZ can be anything (it could be 2, or 16, or whatever) it will still work the same, only larger numbers are more efficient. There is no point using less than 1K even if the input is usually short, IMO. Also, if your target OS can return NULL on malloc/realloc you will want to do "out of memory" error checking.
You can adapt the function to say, sscanf different parts of the line and return a struct instead.