[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 919
  • Last Modified:

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

0
luhres
Asked:
luhres
  • 4
  • 3
  • 2
1 Solution
 
luhresAuthor Commented:
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
0
 
ahoffmannCommented:
shurly no vsscanf() ? It should be in stdarg.h .

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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dirgeCommented:
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!
0
 
luhresAuthor Commented:
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.

0
 
dirgeCommented:
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
0
 
luhresAuthor Commented:
Never mind ... :-)

I use VRTX run-time library ...

Laurent
0
 
dirgeCommented:
Here I am again, this time *meaning* to answer ;)

Here's a promising URL:

http://www.npac.syr.edu/projects/cps615fall96/examples/ppmpi_c/chap08/vsscanf.c

The .h is also in that directory. Although the code is longer than your tentative solution, and it uses two functions, it seems to be pretty much portable. Just remove all those DEBUG lines.

Hope this is it! :)
0
 
luhresAuthor Commented:
Hi Dirge,

thanks a lot for your fast response ...

Laurent
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now