locked
Analyzing a string with spaces using sscanf RRS feed

  • Question

  • I have a configuration file that has IP addresses (space separated). I have to read the config file and gather all the IPs into a single string.

    IP = IP1 IP2 IP3

    I havent been able to use sscanf to read strings with whitespace characters. How can I do this??

    Thanks for your help!!

    Friday, October 13, 2006 4:06 PM

Answers

  • Does this help?


    #include <stdio.h>

    int main()
    {
        const char* example_spaced = "129 101 24 67";
        const char* example_dotted = "129.101.24.67";

        int ip[4];
        int count = sscanf_s( example_spaced, "%d %d %d %d", &ip[0], &ip[1], &ip[2], &ip[3] );

        if( count < 4 )
        {
            fprintf( stderr, "incomplete IP address (spaced)\n" );
        }
        else
        {
            printf( "Result of spaced address (in dotted format): %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3] );
        }

        count = sscanf_s( example_dotted, "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3] );

        if( count < 4 )
        {
            fprintf( stderr, "incomplete IP address (dotted)\n" );
        }
        else
        {
            printf( "Result of dotted address (in dotted format): %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3] );
        }

        return 0;
    }

    If you can have a combination of spaces as well as dots (e.g.  "129. 101   . 24 .67"), then you can may consider using strtok using period and spaces as delimiters, but this of course will be quite lenient.

    Brian

    Thursday, October 19, 2006 3:46 AM

All replies

  • It's not really clear to me what you want to do.  What are you working with?  What does the string you're trying to extract information from look like?  What information would you like to extract?  What do you want to have in the end?

     

    From what you've said so far, I can't figure out why you're trying to use sscanf.

     

     

    Wednesday, October 18, 2006 7:22 PM
  • Does this help?


    #include <stdio.h>

    int main()
    {
        const char* example_spaced = "129 101 24 67";
        const char* example_dotted = "129.101.24.67";

        int ip[4];
        int count = sscanf_s( example_spaced, "%d %d %d %d", &ip[0], &ip[1], &ip[2], &ip[3] );

        if( count < 4 )
        {
            fprintf( stderr, "incomplete IP address (spaced)\n" );
        }
        else
        {
            printf( "Result of spaced address (in dotted format): %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3] );
        }

        count = sscanf_s( example_dotted, "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3] );

        if( count < 4 )
        {
            fprintf( stderr, "incomplete IP address (dotted)\n" );
        }
        else
        {
            printf( "Result of dotted address (in dotted format): %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3] );
        }

        return 0;
    }

    If you can have a combination of spaces as well as dots (e.g.  "129. 101   . 24 .67"), then you can may consider using strtok using period and spaces as delimiters, but this of course will be quite lenient.

    Brian

    Thursday, October 19, 2006 3:46 AM