I'll have a Variable argument list to go...
Hello,
As I've now realised using scanf is a false economy (For want of a better word :P), I've been trying to encapsulate fgets and sscanf into a single function. My only problem with this is that after remembering the declaration for scanf uses the evil ellipsis (...), signifying a bevvy of arguments that the system never has much idea as to the length of, I screamed "NOOOOOOOOOOOOO!" and rammed my fist into my keyboard, snapping it in two. Anyways, a new keyboard later... :o
I've had a go at making the function, like so:-
Code:
int ReadInput(char *szOutput, int iSize, const char *szInput, ...)
{
const char *pTemp;
va_list ap;
void *args = NULL;
unsigned long *arg;
int iArgs = 0;
va_start(ap, szInput);
fgets(szOutput, iSize, stdin);
pTemp = szInput;
while (*pTemp != '\0')
{
if (*(pTemp++) == '%')
{
args = realloc(args, (iArgs + 1) * sizeof(void *));
arg = (unsigned long *)args + iArgs++ * sizeof(void *);
*arg = (unsigned long)va_arg(ap, void *);
}
}
va_end(ap);
sscanf(szOutput, szInput, args);
free(args);
return 0;
}
The variable arguments of ReadInput should be the same as that of sscanf, so they are all addresses to variables, hence me attempting to collect the addresses together and present them to sscanf myself. This doesn't work hoewever as "args" is now doubt treated as an argument in itself... :(
Is there perhaps a low-level method of pushing all the arguments in with scanf, or am I stuck until I get hold of a C99-compliant compiler?