preserving format

  • Thread starter Duncan Muirhead
  • Start date
D

Duncan Muirhead

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?
TIA
Duncan
 
A

Arthur J. O'Dwyer

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top