-
October 3rd, 2003, 10:25 PM
#1
How to check whether a buffer contains a double number?
Hello, everyone!
How to use C (not C++) to check whether a character buffer (char*) contains a double or float number? I want to know how to check it with standard C (not C++) method and not VC++ specific method.
For example,
----------
char buf1 [5] = "123.4";
char buf2 [5] = "123.m";
----------
What the result I want to get is, when I input buf1, I can get TRUE and when I input buf2, I can get FALSE.
I find the function atof is not working properly.
Thanks in advance,
Geo
-
October 4th, 2003, 04:40 AM
#2
Re: How to check whether a buffer contains a double number?
Originally posted by George2
I find the function atof is not working properly.
What do you mean with 'not working properly'?
-
October 4th, 2003, 04:52 AM
#3
I guess I know what you mean with
atof is not working properly.
for atof the string "123." is a valid float.( even "123" is valid although it is more like an integer ).
if you want different behavior you have to set up your own parser. use regular expressions.
kurt
-
October 4th, 2003, 05:09 AM
#4
Re: Re: How to check whether a buffer contains a double number?
Thanks, Andreas buddy!
I want atof returns an error status other than 123. when the input is 123.m.
But the function atof returns the most longest string that matches a double/float number rule. It can be used to check whether the beginning of a string is a double/float number, but can not be used to check whether the whole string is a legal double/float number.
Can you help?
regards,
George
Originally posted by Andreas Masur
What do you mean with 'not working properly'?
-
October 4th, 2003, 05:12 AM
#5
Thanks, ZuK buddy!
Yes, it is just what I mean. Is there a standard C method that can be used to meet my purpose other than regular expression? I think the library of regular expression is too large for a tiny program which is only several thousand lines.
regards,
George
Originally posted by ZuK
I guess I know what you mean with
for atof the string "123." is a valid float.( even "123" is valid although it is more like an integer ).
if you want different behavior you have to set up your own parser. use regular expressions.
kurt
-
October 4th, 2003, 05:18 AM
#6
You can write a function to check every char in the array is either a number char or '.' char before feeding it to atof()
-
October 4th, 2003, 07:59 AM
#7
You can use stdtod() ... the only caveat, depending on what
you want, you might need to remove trailing blanks from
the string if you don't want bthat flagged as an error ...
Code:
#include <stdlib.h>
#include <stdio.h>
int main()
{
double x;
char * endptr;
char * str = "1.0m";
x = strtod(str,&endptr);
if (endptr != NULL)
printf("stopped scan at: %s\n",endptr);
return 0;
}
-
October 4th, 2003, 09:50 PM
#8
Thanks, CBasicNet buddy!
Geo
Originally posted by CBasicNet
You can write a function to check every char in the array is either a number char or '.' char before feeding it to atof()
-
October 4th, 2003, 09:57 PM
#9
Thanks, Philip buddy!
Geo
Originally posted by Philip Nicoletti
You can use stdtod() ... the only caveat, depending on what
you want, you might need to remove trailing blanks from
the string if you don't want bthat flagged as an error ...
Code:
#include <stdlib.h>
#include <stdio.h>
int main()
{
double x;
char * endptr;
char * str = "1.0m";
x = strtod(str,&endptr);
if (endptr != NULL)
printf("stopped scan at: %s\n",endptr);
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|