C – using sscanf to parse input

Background

Let us use the C function, sscanf, to parse input.

Bible verse

Let us say we want to accept bible verses and interpret the input.

Example Input

Genesis 3:1

sscanf

Format

%s %d:%d

Code

Outline

  1. Function – requestVerseInput
    • Prompt :-
      • printf
    • Accept Input
      • gets
    • Display
      • Display accepted input
  2. Function – parseVerseInput
    • Reset variables
    • sscanf
      • Accepts input
        • Format :- %s %d:%d
      • Result
        • return code :- Number of arguments accepted
      • Ramification
        • If number of arguments accepted is equal to accepted
          • Store sscanf result in function variables

Header & Global Definitions


#include <stdio.h>

#define debugSelf 0
//#define debugSelf 1

const int iRC_SSCANF_VERSE_EXPECTED_COUNT = 3;

const char CHAR_TAB[3]="\t";

Function – requestVerseInput


int requestVerseInput
(
      int  iPrompt
    , char *verse
)
{

    const char prompt[] = "Please enter verse %d) (Sample :- %s) :- ";
    const char syntax[] = "Book chapter:verse";
    const char feedback[] = "\nVerse entered is %d) %s \n";

    printf
    (
          prompt
        , iPrompt
        , syntax
    );

    gets(verse);

    printf
    (
          feedback
        , iPrompt
        , verse
    );

    return 0;
}

Function – parseVerseInput


int parseVerseInput
(
      int   iPrompt
    , char  *verseFull
    , int   useLocalVariables
    , char  *bookName
    , int   *chapterNumber
    , int   *verseNumber
)
{

    const char parser[] = "%s %d:%d";
    const char preparingSSCANF[] = "%s Preparing sscanf  parser : %s - useLocalVariables :- %d \n"; 
    const char feedbackSSCANF[]  = "%s Feedback sscanf  parser : %s - rc  :- %d \n";
    const char parserFailed[]    = "%s Expected %d, but captured %d\n";
    
    int iRC=0;
    int chapterNumberLocal =0;
    int verseNumberLocal=0;
    
    // reset input variables
    strcpy(bookName, "");
    *chapterNumber = 0;
    *verseNumber =0;
    
    /*
        sscanf
    */
    if (debugSelf == 1)
    {
        
        printf
            (
                  preparingSSCANF
                , CHAR_TAB
                , parser
                , useLocalVariables
            );          

    }
    
    if (useLocalVariables==1)
    {

        
        iRC = sscanf
        (
              verseFull
            , parser        
            , bookName
            , &chapterNumberLocal
            , &verseNumberLocal
        );
        
    }
    else
    {

        iRC = sscanf
        (
              verseFull
            , parser        
            , bookName
            , chapterNumber
            , verseNumber
        );
        
    }

    if (debugSelf == 1)
    {
        
        printf
            (
                  feedbackSSCANF
                , CHAR_TAB
                , parser
                , iRC
            );          

    }
    
    /*
        sscanf :- rc
        if rc matches expected value, then capture local variable in reference variable
    */  
    if (iRC == iRC_SSCANF_VERSE_EXPECTED_COUNT)
    {

        if (useLocalVariables==1)
        {
        
            /*
                Capture Reference by variable
            */
            if (debugSelf == 1)
            {
                    
                printf
                (
                      "%s Copying Local Variabes into function variables...\n"
                    , CHAR_TAB
                );            
                    
            }
            
            *chapterNumber = chapterNumberLocal;            
            *verseNumber = verseNumberLocal;            

            if (debugSelf == 1)
            {
                    
                printf
                (
                      "%s Copied Local Variabes into function variables\n"
                    , CHAR_TAB
                );            
                    
            }
        }
        
        if (debugSelf == 1)
        {
                
            printf("%s Book      :- %s \n", CHAR_TAB, bookName);            
            printf("%s Chapter   :- %d \n", CHAR_TAB, *chapterNumber);              
            printf("%s Verse     - %d \n", CHAR_TAB, *verseNumber);                     
                
        }

    }
    else
    {
    
        printf("%s Input %s \n", CHAR_TAB, verseFull);      
        
        printf("%s Parser %s \n", CHAR_TAB, parser);                
        
        printf(   
                  parserFailed
                , CHAR_TAB
                , iRC_SSCANF_VERSE_EXPECTED_COUNT
                , iRC
             );
        
    }
    
    return iRC;
    
}


Source Code

GitHub

  1. Project
    • DanielAdeniji/ODBCCodeSample
      Link
  2. Source Code
    • bibleVerseInput.c
      Link

 

References

  1. Stackoverflow
    • format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’
      Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s