• C

sscanf: tab character in input string?

Ah hello

I have the following code, not written by myself, designed to parse out field/value pairs from an INI file:
	char txt[100], tmp[100];
	strcpy(txt, "FIELD=VALUE");
	
	if (sscanf( txt, " \t%[^=]", tmp ) == 1)
	{
		// Do stuff with tmp
	}

Open in new window


Now, being reasonably competent with what sscanf does, I find myself confused as to the explicit space and tab character in the input string.  Moreoever, with 'txt' not having any spaces or tab characters in it, I am surprised that 'tmp' still holds "VALUE" after the call.

Can someone explain what the \t and space could be for here please?

TIA
LVL 19
mrwad99Asked:
Who is Participating?
 
ozoCommented:
since %[ supresses the usual skip of leading white space,  the  \t and space explicitly skip any amount of white space, including none
but it seems redundant to have both  \t and space
and tmp should hold "FIELD"  after the call, not "VALUE"
0
 
mrwad99Author Commented:
Thanks!

*Sigh*, yes: Ozo sorry the value of tmp is of course FIELD.

Yes, according to http://linux.die.net/man/3/sscanf it seems that using [ will supress skipping of leading white space.  (Not that I doubted you, just for others reading :))

But why the explicit single space and \t?  I tried with just the space, and just the tab, and both worked the same...any idea?
0
 
ozoCommented:
perhaps the author did not know that either would suffice
0
 
mrwad99Author Commented:
Thank you very much :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.