sscanf

  • Thread starter Thread starter Evan Mathias
  • Start date Start date
E

Evan Mathias

Does anyone know a equivalent to sscanf from c++.

I would appreciate a good answer to this one, since currently I have had to
write a library of regular expressions to recognise integers, floats etc..
and I dont want to use exceptions or anything else that is slow or
impractical.



Much thanks.



Evan
 
Evan said:
Does anyone know a equivalent to sscanf from c++.

I would appreciate a good answer to this one, since currently I have
had to write a library of regular expressions to recognise integers,
floats etc.. and I dont want to use exceptions or anything else that
is slow or impractical.

Well I was going to suggest the regex classes, but then I read you
didn't want to use anything slow and impractical ;-)

So assuming you don't want to use the regex classes I think that what
you want is a combination of the split method of the string and then
the appropriate convert routine. i.e

/*untested, as it was written in the news reader*/

string[] theValues = buffer.Split(**yourDelims**);

double theDoubleValue = Convert.ToDouble(theValues[0]);
int32 theIntValue = Convert.ToInt32(theValues[1]);

etc, etc

Obviously you would have the appropriate error checking and handling.

Cheers Tim.
 
Thanks Tim

I have found the regex class fast, my real issue was the amount of code
required to build a library and it was not really appropriate for the same
things sscanf was used to do. My options were to implement regex, use split
or bring across scannf from c++. Split seemed to be really quite slow, since
I would normally use something like



If (Sscanf(str, "%i, %f FACTOR 4s %f), a, b, c, d) == 4) {}



Hence using split and convert would generate many exceptions, and thus have
a massive performance hit. I assume there must be an option in managed code.


Tim Jarvis said:
Evan said:
Does anyone know a equivalent to sscanf from c++.

I would appreciate a good answer to this one, since currently I have
had to write a library of regular expressions to recognise integers,
floats etc.. and I dont want to use exceptions or anything else that
is slow or impractical.

Well I was going to suggest the regex classes, but then I read you
didn't want to use anything slow and impractical ;-)

So assuming you don't want to use the regex classes I think that what
you want is a combination of the split method of the string and then
the appropriate convert routine. i.e

/*untested, as it was written in the news reader*/

string[] theValues = buffer.Split(**yourDelims**);

double theDoubleValue = Convert.ToDouble(theValues[0]);
int32 theIntValue = Convert.ToInt32(theValues[1]);

etc, etc

Obviously you would have the appropriate error checking and handling.

Cheers Tim.
 
Evan said:
thus have a massive performance hit. I assume there must be an option
in managed code.

I'm not sure there is a close equivalent, not that I know of anyway.

Cheers Tim
 
Back
Top