By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

Needed help for strtof return value

P: n/a
Dear Group

I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error
messages. It is my understanding that strtof returns the converted
value if the conversion is successful a "0" upon failure. When this
conversion fails, my program sends an error message to my error.log
file. The problem with this is there are times when strtof converts a
text string such as "0.000000" to a float. The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message. This can happen hundreds if not thousands
of times. Because of this, it is not feasible to check all of the
messages to see if the conversion failed. Does anyone have any ideas
as to how I can test for a successful strtof conversion for my
situation.
Regards

Marcus D. Jacobs
Nov 13 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
In article <96**************************@posting.google.com >, Marcus Jacobs wrote:
Dear Group

I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error
messages. It is my understanding that strtof returns the converted
value if the conversion is successful a "0" upon failure. When this
conversion fails, my program sends an error message to my error.log
file. The problem with this is there are times when strtof converts a
text string such as "0.000000" to a float. The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message. This can happen hundreds if not thousands
of times. Because of this, it is not feasible to check all of the
messages to see if the conversion failed. Does anyone have any ideas
as to how I can test for a successful strtof conversion for my
situation.

Sure: compare the string you tried to convert with 0.000000. If you can have
any number of zeroes before and after the dot, you can do something like this:

int is_zero(const char * str)
{
int i;
int found_dot = 0;
for (i = 0; str[i]; i++)
{
if (str[i] == '.')
{
if (found_dot)
return -1; /* error */
found_dot++;
continue;
}
if (str[i] != '0')
return(0);
}

return(1);
}

HTH

rlc

--
Jail: Just Another Interpreted Language
Just: Jail Uses Silly Terms

Join the discussion on the definition of this language at
ja***********@lists.sourceforge.net http://jail-ust.sourceforge.net
(send mail to ja*********************@lists.sourceforge.net)
Nov 13 '05 #2

P: n/a
ma*******@hotmail.com (Marcus Jacobs) wrote in
news:96**************************@posting.google.c om:
Dear Group

I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error
messages. It is my understanding that strtof returns the converted
value if the conversion is successful a "0" upon failure. When this
conversion fails, my program sends an error message to my error.log
file. The problem with this is there are times when strtof converts a
text string such as "0.000000" to a float. The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message.


Returning zero is not the only thing strtof does upon failing. Read the
docs about the second parameter. Then, do a proper error check in your code
rather than one that only goes halfway.

Sinan.

Nov 13 '05 #3

P: n/a
ma*******@hotmail.com (Marcus Jacobs) wrote in
news:96**************************@posting.google.c om:
Dear Group

I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error
messages. It is my understanding that strtof returns the converted
value if the conversion is successful a "0" upon failure. When this
conversion fails, my program sends an error message to my error.log
file. The problem with this is there are times when strtof converts a
text string such as "0.000000" to a float. The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message.


Returning zero is not the only thing strtof does upon failing. Read the
docs about the second parameter. Then, do a proper error check in your code
rather than one that only goes halfway.

Sinan.

Nov 13 '05 #4

P: n/a
ma*******@hotmail.com (Marcus Jacobs) wrote:
Dear Group

I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error
messages. It is my understanding that strtof returns the converted
value if the conversion is successful a "0" upon failure. When this
conversion fails, my program sends an error message to my error.log
file. The problem with this is there are times when strtof converts a
text string such as "0.000000" to a float. The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message. This can happen hundreds if not thousands
of times. Because of this, it is not feasible to check all of the
messages to see if the conversion failed. Does anyone have any ideas
as to how I can test for a successful strtof conversion for my
situation.


The following may help you to solve this problem:

<quote>

From ISO/IEC 9899:1999

7.20.1.3 The strtod, strtof, and strtold functions
[...]
float strtof(const char * restrict nptr, char ** restrict endptr);
[...]
#7
If the subject sequence is empty or does not have the expected form, no
conversion is performed; the value of nptr is stored in the object
pointed to by endptr, provided that endptr is not a null pointer.
[...]
#10
The functions return the converted value, if any. If no conversion could
be performed, zero is returned. If the correct value is outside the
range of representable values, plus or minus HUGE_VAL, HUGE_VALF, or
HUGE_VALL is returned (according to the return type and sign of the
value), and the value of the macro ERANGE is stored in errno. If the
result underflows (7.12.1), the functions return a value whose magnitude
is no greater than the smallest normalized positive number in the return
type; whether errno acquires the value ERANGE is implementation-defined.

</quote>

Thus, you can check errno to see if an error occurred, and you can
examine endptr to check how much of your string was processed.

Regards

Irrwahn
--
Great minds run in great circles.
Nov 13 '05 #5

P: n/a
ma*******@hotmail.com (Marcus Jacobs) wrote:
Dear Group

I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error
messages. It is my understanding that strtof returns the converted
value if the conversion is successful a "0" upon failure. When this
conversion fails, my program sends an error message to my error.log
file. The problem with this is there are times when strtof converts a
text string such as "0.000000" to a float. The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message. This can happen hundreds if not thousands
of times. Because of this, it is not feasible to check all of the
messages to see if the conversion failed. Does anyone have any ideas
as to how I can test for a successful strtof conversion for my
situation.


The following may help you to solve this problem:

<quote>

From ISO/IEC 9899:1999

7.20.1.3 The strtod, strtof, and strtold functions
[...]
float strtof(const char * restrict nptr, char ** restrict endptr);
[...]
#7
If the subject sequence is empty or does not have the expected form, no
conversion is performed; the value of nptr is stored in the object
pointed to by endptr, provided that endptr is not a null pointer.
[...]
#10
The functions return the converted value, if any. If no conversion could
be performed, zero is returned. If the correct value is outside the
range of representable values, plus or minus HUGE_VAL, HUGE_VALF, or
HUGE_VALL is returned (according to the return type and sign of the
value), and the value of the macro ERANGE is stored in errno. If the
result underflows (7.12.1), the functions return a value whose magnitude
is no greater than the smallest normalized positive number in the return
type; whether errno acquires the value ERANGE is implementation-defined.

</quote>

Thus, you can check errno to see if an error occurred, and you can
examine endptr to check how much of your string was processed.

Regards

Irrwahn
--
Great minds run in great circles.
Nov 13 '05 #6

P: n/a
On 24 Sep 2003 07:56:40 -0700, ma*******@hotmail.com (Marcus Jacobs) wrote:
I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error
Perhaps you meant strtod?
file. The problem with this is there are times when strtof converts a
text string such as "0.000000" to a float. The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message. This can happen hundreds if not thousands


strtod is prototyped as

double strtod(const char *s, char **endptr)

On error, *endptr (if not NULL) points to the character that failed the
conversion. So, instead of relying on the return value from strtod, use
the endptr argument:

#include <stdio.h>
#include <stdlib.h>

int cvtfstr(const char *s, float *num, char **errptr)
{
char *endptr;

*errptr = NULL;
*num = (float) strtod(s, &endptr);

/* On error (endptr not at end of string), return zero */
/* Also set errptr to point to errant character */

if (*endptr != '\0')
{
*errptr = endptr;
return 0;
}

return 1;
}
#define NUM_NUMSTR 10

int main(void)
{
const char *numstr[NUM_NUMSTR] =
{
"22.32873", "0.00", "23-skiddoo",
"1954", ".732", " 13 ",
" 13", "-1234.56e-2", "+ 951",
"+159"
};

size_t i;
float num;
char *errptr;

for (i = 0; i < NUM_NUMSTR; i++)
{
printf("s=\"%s\"\t\tnum=", numstr[i]);

if (cvtfstr(numstr[i], &num, &errptr))
printf("%f\n", num);
else
printf("ERROR AT OFFSET %p (%s)\n",
errptr - numstr[i], errptr);
}

return 0;
}
--
Robert B. Clark (email ROT13'ed)
Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/
Nov 13 '05 #7

P: n/a
On 24 Sep 2003 07:56:40 -0700, ma*******@hotmail.com (Marcus Jacobs) wrote:
I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error
Perhaps you meant strtod?
file. The problem with this is there are times when strtof converts a
text string such as "0.000000" to a float. The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message. This can happen hundreds if not thousands


strtod is prototyped as

double strtod(const char *s, char **endptr)

On error, *endptr (if not NULL) points to the character that failed the
conversion. So, instead of relying on the return value from strtod, use
the endptr argument:

#include <stdio.h>
#include <stdlib.h>

int cvtfstr(const char *s, float *num, char **errptr)
{
char *endptr;

*errptr = NULL;
*num = (float) strtod(s, &endptr);

/* On error (endptr not at end of string), return zero */
/* Also set errptr to point to errant character */

if (*endptr != '\0')
{
*errptr = endptr;
return 0;
}

return 1;
}
#define NUM_NUMSTR 10

int main(void)
{
const char *numstr[NUM_NUMSTR] =
{
"22.32873", "0.00", "23-skiddoo",
"1954", ".732", " 13 ",
" 13", "-1234.56e-2", "+ 951",
"+159"
};

size_t i;
float num;
char *errptr;

for (i = 0; i < NUM_NUMSTR; i++)
{
printf("s=\"%s\"\t\tnum=", numstr[i]);

if (cvtfstr(numstr[i], &num, &errptr))
printf("%f\n", num);
else
printf("ERROR AT OFFSET %p (%s)\n",
errptr - numstr[i], errptr);
}

return 0;
}
--
Robert B. Clark (email ROT13'ed)
Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/
Nov 13 '05 #8

P: n/a
Marcus Jacobs <ma*******@hotmail.com> wrote:

The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message.


As you've discovered, checking the return value is not a good way to
determine whether the conversion was successful or not. That's what the
second argument is for:

char *endptr;

f = strtof(buf, &endptr);
if (endptr == buf) // an error occurred

-Larry Jones

Well of course the zipper's going to get stuck if everyone
stands around WATCHING me! -- Calvin
Nov 13 '05 #9

P: n/a
Marcus Jacobs <ma*******@hotmail.com> wrote:

The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message.


As you've discovered, checking the return value is not a good way to
determine whether the conversion was successful or not. That's what the
second argument is for:

char *endptr;

f = strtof(buf, &endptr);
if (endptr == buf) // an error occurred

-Larry Jones

Well of course the zipper's going to get stuck if everyone
stands around WATCHING me! -- Calvin
Nov 13 '05 #10

P: n/a
ma*******@hotmail.com (Marcus Jacobs) writes:
I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error
messages. It is my understanding that strtof returns the converted
value if the conversion is successful a "0" upon failure. When this
conversion fails, my program sends an error message to my error.log
file. The problem with this is there are times when strtof converts a
text string such as "0.000000" to a float. The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message. This can happen hundreds if not thousands
of times. Because of this, it is not feasible to check all of the
messages to see if the conversion failed. Does anyone have any ideas
as to how I can test for a successful strtof conversion for my
situation.


The strtof() function takes two arguments, a char* pointing to the
string to be converted, and a char** pointing to a pointer which is
set to point past the converted string.

For example:

char *s = "123 456";
char *endptr;
float f = strtof(s, &endptr);

This will set f to 123.0, and set endptr to point to the blank in the
string s (i.e., just past the portion of the input string that it was
able to convert).

If strtof() wasn't able to make a conversion (i.e., the argument
string doesn't start with zero or more whitespace characters followed
by a valid floating-point constant), strtof() will return 0.0 and
endptr will be set to point to the beginning of the argument string.

After the call, if endptr == s, the call failed; otherwise, it
succeeded.

You can also check errno to determine why the call failed (assuming
you've set errno to 0 before the call), but remember that errno is not
set if the call fails because the string didn't contain a
floating-point constant.

--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #11

P: n/a
Robert B. Clark <ep****@3pynexf.pbz> writes:
On 24 Sep 2003 07:56:40 -0700, ma*******@hotmail.com (Marcus Jacobs) wrote:
I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error


Perhaps you meant strtod?


The C90 standard defined the strtod function (returning a double).
The C99 standard adds strtof (returning a float) and strtold
(returning a long double) functions.

--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.