scanf - Read string and various integers in same line in C -


i have string called buffer has following data stored:
rb [7, 0] 64

using sscanf(), i'd following:

  • read rb , store in string called name
  • read 7 , store in int variable called posx
  • read 0 , store in int variable called posy
  • read 64 , store in int variable called battery_level

i tried following, doesn't work:

sscanf(buffer, "%s[^\ ] [%d,%d] %d", name, &posx, &posy, &battery_level); 

problems see:

  1. "\ " not valid escape sequence.
  2. "%s[^ ]" not expecting do. need use "%[^ ]".

you can use

sscanf(buffer, "%s [%d,%d] %d", name, &posx, &posy, &battery_level); 

or

sscanf(buffer, "%[^ ] [%d,%d] %d", name, &posx, &posy, &battery_level); 

both of them work. see working code @ http://ideone.com/qnuquy


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -