Solved

sscanf with va_list

Posted on 1997-08-29
9
880 Views
Last Modified: 2008-03-17
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
Comment
Question by:luhres
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 

Author Comment

by:luhres
ID: 1253790
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1253791
shurly no vsscanf() ? It should be in stdarg.h .

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1253792
shurely no vsscanf() ? It should be in stdarg.h .

0
Independent Software Vendors: 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!

 
LVL 1

Expert Comment

by:dirge
ID: 1253793
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
 

Author Comment

by:luhres
ID: 1253794
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
 
LVL 1

Expert Comment

by:dirge
ID: 1253795
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
 

Author Comment

by:luhres
ID: 1253796
Never mind ... :-)

I use VRTX run-time library ...

Laurent
0
 
LVL 1

Accepted Solution

by:
dirge earned 100 total points
ID: 1253797
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
 

Author Comment

by:luhres
ID: 1253798
Hi Dirge,

thanks a lot for your fast response ...

Laurent
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question