473,473 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Needed help for strtof return value

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
11 3032
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sarah | last post by:
Hi all. I have a form, and several text and image links on it that should submit the form with different actions. I prepared a simple page with just the code that's not working. PROBLEM:...
3
by: % =joe % | last post by:
I cannot get this code to work. Very simple...I have three list menus. I want to do a check before the form submits to make sure that the value of the 3 fields is equal to 12. Here's my...
4
by: Nitin | last post by:
Hi I have created function to check date and time. at the time of execution, if date is left empty the function returns the error message but then the focus goes to next field. Next filed is for...
6
by: Jamal | last post by:
I am working on binary files of struct ACTIONS I have a recursive qsort/mergesort hybrid that 1) i'm not a 100% sure works correctly 2) would like to convert to iteration Any comments or...
11
by: Marcus Jacobs | last post by:
Dear Group I am encountering a precision issue while converting a text string to a float using STRTOF. For example, at times a text string "736.00000" (quotation marks added) is converted to...
25
by: David Bernier | last post by:
I'd like to pass on the command line two filenames. As for example: my_executable filename_1 filename_2 I haven't done any C programming with command line arguments so far. I'm familiar...
10
by: Jonathan Lamothe | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I've written a program that requires the user to input a decimal number which is then stored as a float. First it's read into a variable buf...
2
by: Eric Lilja | last post by:
Hello, consider this complete program: #include <iostream> #include <string> using std::cout; using std::endl; using std::string; class Hanna {
4
by: EricW | last post by:
Hi, I found the following code in a FAQ about the datagridview. But I am having a hard time translating it to VB, so I would like to ask via here for someone more experienced to translate this...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.