On Thu, 1 Feb 2007, Duncan Muirhead wrote:
>
I'm writing a wee utility that applies some simple transforms (eg adds
offsets) to some (text file) fields that represent floating point numbers.
Simple enough. However I would like to preserve the input format, e.g. if
the input looks like %15.7e then so should the output. Is there any
cunning use of e.g. sscanf to get not just the number, but also the
format?
No, I don't think so. The best solution is probably just to parse the
numeric fields yourself, using 'isdigit' and 'strchr' and so on. Then
you can extract all the formatting information you need, and
simultaneously avoid undefined or unwanted behavior when the user starts
entering "numbers" like "+35798735738539757.2b".
If you don't want to actually compute the floating-point value yourself
(by successively adding multiples of powers of ten), you could also
parse the field yourself to start with, and then pass the original string
to 'sscanf' to compute the actual floating-point value.
-Arthur