vsscanf - An Implementation for Windows

Environment: VC++ 6.0, Windows 9x/ME/NT/2000

VC++ provides the vsprintf() function but for some strange reason does not provide the complementary vsscanf() function which is available on most Unix systems. The VC++ library defines a static function called input() which is not exposed to end users. This function is exactly what vsscanf() does.

There are two choices that one has:

  • Extract the input() source from the VC++ CRT sources and use it in your code.
  • Use this alternative implementation
Several C++ implementations exist to plug this hole but most of them do not provide the identical API available on Linux. This implementation enhances the CStringEx::Scanf extension written by Rainer Bauer to provide an identical API to that which is available on Linux.

An example usage of vsscanf() (in a Win32 console application with MFC support) is shown below:

#include "stdafx.h"
#include "vsscanf.h"

CWinApp theApp;

using namespace std;

// ProcessNumEmailMsgs takes variable number of parameters and
// uses vsscanf to parse the parameters.
void ProcessNumEmailMsgs(char *input, char *format, ...)
{
  va_list argList;

  va_start(argList, format);
  vsscanf(input, format, argList);
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
  int nRetCode = 0;

  // initialize MFC and print and error on failure
  if (!AfxWinInit( ::GetModuleHandle(NULL),
                   NULL, 
                   ::GetCommandLine(),
                   0))
  {
    // TODO: change error code to suit your needs
    cerr << _T("Fatal Error: MFC initialization failed") << endl;
    nRetCode = 1;
  }
  else
  {
    int startMsg, endMsg, totalMsg;
    char *input = "Showing 1 - 10 of 100";
    char *format = "Showing %d - %d of %d";

    // Variable Number of parameters are passed to this
    // function
    ProcessNumEmailMsgs(input, format, &startMsg, &endMsg, &totalMsg);

    printf("Input String: %s\n", input);
    printf("StartMsg    : %d\n", startMsg);
    printf("EndMsg      : %d\n", endMsg);
    printf("TotalMsg    : %d\n", totalMsg);
  }

  return nRetCode;
}

Downloads

Download source and demo project - 12 Kb


Comments

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • You probably have several goals for your patient portal of choice. Is "community" one of them? With a bevy of vendors offering portal solutions, it can be challenging for a hospital to know where to start. Fortunately, YourCareCommunity helps ease the decision-making process. Read this white paper to learn more. "3 Ways Clinicians can Leverage a Patient Portal to Craft a Healthcare Community" is a published document owned by www.medhost.com

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds