Solved

sscanf with va_list

Posted on 1997-08-29
9
857 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now