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

javascript - three.js lot of meshes optimization -

smartface.io - Proper way to change color scheme for whole application -

Email notification in google apps script -