PDA

View Full Version : fscanf: using a constant as a format width specifier?


why_bird
12-05-2008, 01:03 PM
Hello all,
I want to use fscanf with a width specifier to avoid buffer overflow. I'm looking to do something like the following, but with something that works replacing [STR_LEN], so that I can avoid hard-coding the length of the string. is there any way to do this?

#define STR_LEN 256
FILE * COMMAND
char * line=calloc(STR_LEN,sizeof(char));
...
fscanf(COMMAND,"%[STR_LEN]s",line);
I saw some code which I think claimed to do this which did something like

fscanf(COMMAND,"%"xstr(STR_LEN)"s",line);
but I a) can't remember if that's exactly what it was and b) can't see how it should work. needless to say that neither works at the moment.
Thanks,
why_bird