Link to home
Start Free TrialLog in
Avatar of luhres
luhres

asked on

sscanf with va_list

Hi folks,

Here is what I want to do

void MySscanf( char*    pcp_string, const char*  pcp_format,
 ... )
{
  va_list   marker;

  va_start( marker, pcp_format);

 sscanf(pcp_string, pcp_format, marker ); /* that is impossible :-( */

 va_end( marker );
}

As the prototype of sscanf is

sscanf( const char *buffer, const char *format [, argument ] ... );

it is not possible to pass an argument list to it.

And there is no

vsscanf( const char *buffer, const char *format,  va_list argptr );

Why isn't there such a function? And what should I do?

Any ideas are welcom.

Thanks.

Laurent

Avatar of luhres
luhres

ASKER

Here is a solution working for Microsoft C ... I took a look into the
source files and found this in sscanf.c

int __cdecl sscanf (
        REG2 const char *string,
        const char *format,
        ...
        )
/*
 * 'S'tring 'SCAN', 'F'ormatted
 */
{
        va_list arglist;
        FILE str;
        REG1 FILE *infile = &str;
        REG2 int retval;

        va_start(arglist, format);

        _ASSERTE(string != NULL);
        _ASSERTE(format != NULL);

        infile->_flag = _IOREAD|_IOSTRG|_IOMYBUF;
        infile->_ptr = infile->_base = (char *) string;
        infile->_cnt = strlen(string);

        retval = (_input(infile,format,arglist));

        return(retval);
}

So it looks obvious to me to think about this:

int __cdecl vsscanf (
        REG2 const char *string,
        const char *format,
        va_list arglist
        )
/*
 * 'S'tring 'SCAN', 'F'ormatted
 */
{
        FILE str;
        REG1 FILE *infile = &str;
        REG2 int retval;

        _ASSERTE(string != NULL);
        _ASSERTE(format != NULL);

        infile->_flag = _IOREAD|_IOSTRG|_IOMYBUF;
        infile->_ptr = infile->_base = (char *) string;
        infile->_cnt = strlen(string);

        retval = (_input(infile,format,arglist));

        return(retval);
}

I have tried it (you need to include some internal header files and to
define _CRTBLD to avoid error "ERROR: Use of C runtime library internal
header file." at precompile time) and it works ... but only for
Microsoft C ... where is portability???? ... So if there is someone out
there who knows what the guys who developped the standard run-time
library where thinking at that day, he just ought to let the world know
about it. Because why is there vfprintf() for fprintf() (vsprintf() for
sprintf()) and none for scanf() (sscanf())???????????

Laurent
shurly no vsscanf() ? It should be in stdarg.h .

shurely no vsscanf() ? It should be in stdarg.h .

Here's the UNIX stdio.h entry (only if strict ANSI is not required):

extern int vsscanf __P((__const char *, __const char *, _G_va_list));

For Turbo C:
int vsscanf(const char *buffer, const char *format, va_list arglist);

which is pretty much what you have, so you should have no problem working with it. Perhaps you've been requiring strict ANSI compliance and that's why it doesn't "show up". Good luck!
Avatar of luhres

ASKER

Hi Dirge,

It's hard to grade your answer because it is not an answer. It's merely an observation that such a function exists in other systems. The system I work on does _not_ have such a function so I am still waiting for an implementation answer.

Oops. I meant that as a comment. <blush> I guess I forgot to "move back" the option; just go ahead and reject it.

In any case, I'm thinking up some code. Mind telling us what particular OS/C compiler you have?

Regards,
dirge
Avatar of luhres

ASKER

Never mind ... :-)

I use VRTX run-time library ...

Laurent
ASKER CERTIFIED SOLUTION
Avatar of dirge
dirge

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of luhres

ASKER

Hi Dirge,

thanks a lot for your fast response ...

Laurent