B
Bill Reid
I just noticed that my "improved" version of sscanf() doesn't assign
floating point numbers properly if the variable assigned to is declared
as a "float" rather than a "double". (This never cropped up before, since
I rarely use "float"s for anything, and hardly ever use the function for
floating-point numbers in the first place; I just was messing around
testing it for all cases and noticed a problem.)
Anyway, it is declared and I assume largely defined the same
way sscanf() is:
int scan_line(char *line,char *format,...) {
va_list var_argument;
/* loop to match up format string with argument list and assign to
pointers */
}
The problem is when I get down to the point of actually assigning
values to the pointers:
switch(*format_buf) {
case 'd' :
copy_max_length_text(number_buf,field,field_length);
*va_arg(var_argument,unsigned long *)=
strtoul(number_buf,NULL,10);
break;
case 'f' :
copy_max_length_text(number_buf,field,field_length);
*va_arg(var_argument,double *)=strtod(number_buf,NULL);
break;
case 's' :
copy_max_length_text
(va_arg(var_argument,char *),field,field_length);
break;
....
"case 'f'" is assigned as 0.0000... if the argument actually points
to a float, but works OK assigned to a double. There may be something
simple I'm missing here, but I get confused enough working with
variable argument list, because of the default promotions of var_arg(),
but I'm not sure that is even the issue here...
I'm assuming it must be possible to do this right, since sscanf()
does it right, but like I say I'm probably missing something very
simple...
floating point numbers properly if the variable assigned to is declared
as a "float" rather than a "double". (This never cropped up before, since
I rarely use "float"s for anything, and hardly ever use the function for
floating-point numbers in the first place; I just was messing around
testing it for all cases and noticed a problem.)
Anyway, it is declared and I assume largely defined the same
way sscanf() is:
int scan_line(char *line,char *format,...) {
va_list var_argument;
/* loop to match up format string with argument list and assign to
pointers */
}
The problem is when I get down to the point of actually assigning
values to the pointers:
switch(*format_buf) {
case 'd' :
copy_max_length_text(number_buf,field,field_length);
*va_arg(var_argument,unsigned long *)=
strtoul(number_buf,NULL,10);
break;
case 'f' :
copy_max_length_text(number_buf,field,field_length);
*va_arg(var_argument,double *)=strtod(number_buf,NULL);
break;
case 's' :
copy_max_length_text
(va_arg(var_argument,char *),field,field_length);
break;
....
"case 'f'" is assigned as 0.0000... if the argument actually points
to a float, but works OK assigned to a double. There may be something
simple I'm missing here, but I get confused enough working with
variable argument list, because of the default promotions of var_arg(),
but I'm not sure that is even the issue here...
I'm assuming it must be possible to do this right, since sscanf()
does it right, but like I say I'm probably missing something very
simple...