1

I am trying to get use sscanf to scan a string of text and store the values into an array. When it comes to storing the last string it stops scanning when it comes to a white space. For example in the below string it would only store the word "STRING". I have tried using %[^ \t\n] and the other various specifiers but it seems I am missing something.

I just cant get the function to include white space, im sure its probably something simple.

string test = "9999:STRING OF TEXT";

scan = sscanf(test, "%d:%s", rec[i].ref, rec[i].string);
newprogrammer
  • 600
  • 10
  • 22

2 Answers2

2

You should have posted a minimal working code.

However, the issue is most likely that %s does not skip white space as do the numerical formats such as %f and %d. Use something like sscanf(test, "%d:%[^\n]", rec[i].ref, rec[i].string); to capture whatever is after :.

Look here for details: [http://www.cplusplus.com/reference/cstdio/sscanf/][1]

gmas80
  • 1,218
  • 1
  • 14
  • 44
1

So this does not work?

sscanf(test, "%d:%[^\t\n]", rec[i].ref, rec[i].string);

check out this answer : reading a string with spaces with sscanf

Basically this will will match the number, followed by anything that is not in the brackets (tab, newline), note the ^ symbol.

Community
  • 1
  • 1
buydadip
  • 8,890
  • 22
  • 79
  • 154