Solved

sscanf with va_list

Posted on 1997-08-29
9
870 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
negation in C function 14 161
Want to delete all my personal data 13 146
valid enum? 6 75
Global Keyboard Hooks Blocked 4 81
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

856 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