Find the answer to your Linux question:
Results 1 to 3 of 3
Hi group, I am fighting with sscanf again. I am trying to use the pointer update feature "%p" and it is not working as expected. The code looks like this: ...
  1. #1
    Just Joined!
    Join Date
    May 2005
    Posts
    15

    trouble with sscanf

    Hi group,

    I am fighting with sscanf again. I am trying to use the pointer update
    feature "%p" and it is not working as expected.

    The code looks like this:

    int
    main( int argc, char* argv[] ) {

    int RC=0;

    int I1,I2,I3,I4;
    char* _P;
    char Buffer[] = "1\n2\n3\n4\n xxzzy The rest of this ! \n";
    char Temp[255];

    strcpy(Temp,Buffer);
    _P = Temp;

    RC=sscanf( _P, "%d\n%d\n%d\n%d\n%p", &I1, &I2, &I3, &I4, _P );
    printf( "RC = %d %d %d %d %d New=%s\n\n", RC, I1, I2, I3, I4, _P );

    strcpy(Temp,Buffer);
    _P = Temp;

    RC=sscanf( _P, "%d\n%d\n%d\n%d\n%s", &I1, &I2, &I3, &I4, _P );
    printf( "RC = %d %d %d %d %d New=%s\n\n", RC, I1, I2, I3, I4, _P );

    return;
    The output looks like this:

    RC = 4 1 2 3 4 New=1
    2
    3
    4
    xxzzy The rest of this !


    RC = 5 1 2 3 4 New=xxzzy
    What I am trying to do is update the pointer to the data after the
    conversion so that _P pointers to "xxzzy The rest of this !"

    Any ideas?

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Format %p in sscanf() is not a pointer update feature. It attempts to read the value of some pointer from the input. The closest to what you want is the %n format.

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    May 2005
    Posts
    15
    Quote Originally Posted by wje_lf View Post
    Format %p in sscanf() is not a pointer update feature. It attempts to read the value of some pointer from the input. The closest to what you want is the %n format.

    Hope this helps.
    Bill,
    Thanks for the reply. Using '%n' is what I did use. Not pretty but it works!
    William

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...