3

I have the following sscanf:

int four;
sscanf("1234&awe$asdf@3222*gr45", format, four);

I want this sscanf to put the value 3222 in the variable four. What should be the format string?

& $ @ and * always stays the same. So other example of string inputs can be:

"6345&346dfh$aweg@5463*nvm" -> and four will be 5463

"0hjgf&456d$fhg@05645*pogyu" -> and four will be 05645

Im sorry if im not clear enough. Im trying to be as clearest as possible.

Edit: I think that the way is this:

sscanf("1234&awe$asdf@3222*gr45", "@%d*", four);

Any other ways?

3 Answers 3

2

Your suggestion won't work, you need first to skip everything to the @, so "%*[^@]@%d"

Sign up to request clarification or add additional context in comments.

2 Comments

Im sorry for beeing noob, But if i want to save the second one as well? What shall I do?
Is it something like "%*[^&]&%s%*[^@]@%d"
2
#include <cstdio>

int main() {
    int four = 0;
    sscanf("1234&awe$asdf@3222*gr45", "%*[^@]@%d%*s", &four);
    printf("%d\n", four);
    return 0;
}

To get second number, you do:

sscanf("1234&awe$asdf@3222*gr45", "%*[^@]@%d%*[^0-9]%d", &four, &four1);

Comments

1

@Nirock comment is on track.

int four;
// Anything not & (don't save it) followed by & then
// Anything not $ (don't save it) followed by $ then
// Anything not @ (don't save it) followed by @ then
// Anything not * (don't save it) followed by * then
// followed by an `int`
if (1 == sscanf(s, "%*[^&]&%*[^$]$%*[^@]@%*[^*]*%d", &four)) {
  ; // found it
}

2 Comments

If i want to do Anything not & or @ (don't save it) Is it simply %*[^&@]?
@Nirock Yes. If you want "Anything not & or @ (don't save it) followed by one & or @ (don't save it)", use "%*[^&@]%*1[&@]". BTW, be careful using the %[], it can be confusing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.