vsscanf - An Implementation for Windows | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
May 20, 2002
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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 110 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: %sn”, input);
    printf(“StartMsg    : %dn”, startMsg);
    printf(“EndMsg      : %dn”, endMsg);
    printf(“TotalMsg    : %dn”, totalMsg);
  }
  return nRetCode;
}

Downloads


Download source and demo project – 12 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.