D
David Mathog
The C string to double conversion functions all seem to operate on the
front of the input string, turning as much of it is as possible into a
number. In a situation where the string should be entirely the
number, and nothing should follow it, the best I have come up with to
handle this case is:
char *atoken;
char *to;
double dtmp;
/* lots of other stuff, generating atoken */
errno=0;
dtmp=strtod(atoken,&to);
if(errno || !to || *to != '\0'){
fprintf(stderr,"fatal error: incorrect number syntax
%s",atoken);
}
It works but isn't very pretty, what with 3 tests for the various
failure modes. Is there another way to go about this, more along the
lines of:
if(cvtS2D(atoken,&dtmp)){
}
?
Where the hypothetical conversion function returns 0 if the input
string is just a number, and something
else for all the other cases?
Thanks,
David Mathog
front of the input string, turning as much of it is as possible into a
number. In a situation where the string should be entirely the
number, and nothing should follow it, the best I have come up with to
handle this case is:
char *atoken;
char *to;
double dtmp;
/* lots of other stuff, generating atoken */
errno=0;
dtmp=strtod(atoken,&to);
if(errno || !to || *to != '\0'){
fprintf(stderr,"fatal error: incorrect number syntax
%s",atoken);
}
It works but isn't very pretty, what with 3 tests for the various
failure modes. Is there another way to go about this, more along the
lines of:
if(cvtS2D(atoken,&dtmp)){
}
?
Where the hypothetical conversion function returns 0 if the input
string is just a number, and something
else for all the other cases?
Thanks,
David Mathog