Skip to main content
GameDev.net gamedev.net
🔒 Locked

sscanf question

Started by digitalfreak Jan 11, 2005 at 9:02 AM 3 replies 850+ views
Original Post
digitalfreak
digitalfreak
I want to read the part between quotation marks of the fowllowing line into destination: shader "models/monsters/zombie/fatty/fatty" I used this: sscanf(currLine, "%*s \"%s\"", destination); but when sscanf returns, destination has one more char than I expected: the last quotation mark. but in the format string, I've already explicitly written the last quotation mark as \", why would it still count it belonging to the %s part?
Enigma
Enigma
Check the definition of sscanf. It's not as smart as you think. Specifically:
scanf types:
s: String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are blank, newline and tab)..

Enigma
Zahlman
Zahlman
You might want to invest in one of these.
Fruny
Fruny
Quote:
Original post by Enigma
scanf types:
s: String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are blank, newline and tab)..


That's not all there is to scanf. Without reaching for a regex library:

#include <stdio.h>int main(){  char destination[256];  // with spaces in the middle, just to show it works.  const char* currLine = "shader \"models/monst  ers/  zombie/fa  tty/f atty\"";  int n = sscanf( currLine, "%*s \"%[^\"]\"", destination);  printf("%d   %s", n, destination);}

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Pipo DeClown
Pipo DeClown
It shows how dumb the forums strings parser is [grin] j/k [attention] appreciation!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.