Date: Fri, 17 May 1996 01:22:45 -0400 (EDT) From: Chuck Robey <chuckr@Glue.umd.edu> To: "Marc G. Fournier" <scrappy@ki.net> Cc: hackers@freebsd.org Subject: Re: C Programming Question Message-ID: <Pine.OSF.3.91.960517012149.5735C-100000@gilligan.eng.umd.edu> In-Reply-To: <Pine.NEB.3.93.960516144301.7805B-100000@freebsd.ki.net>
next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 16 May 1996, Marc G. Fournier wrote:
>
> Hi...
>
> I'm trying to do something that I feel should be relatively
> brain dead to do, but can't get it to work...change an ascii string
> to a double.
>
> I've tried using atof() and strtod(), and both return the
> same answer...0.
>
> The code is simple, and right now, just pulls the first line
> from a file to manipulate it:
Put in the correct includes for strtok (string.h) and strtod (stdlib.h)
and then it will work fine. Your machine has no idea what values those
return.
>
> ---[ short code segment ]----
> #include <stdio.h>
> #include <time.h>
>
> #define DATAFILE "/home/staff/scrappy/snmp/values"
>
> main(int argv, char **argc)
> {
> FILE *in;
> char inbuf[80], *junk, str[20];
> double value;
>
> in=fopen(DATAFILE, "r");
> fgets(inbuf, sizeof(inbuf), in);
> printf("inbuf: %s\n", inbuf);
> junk = strtok(inbuf, " ");
> junk = strtok(NULL, " ");
> strcpy(str, junk);
> printf("%s\n", str);
> value = strtod(str, NULL);
> printf("[%f]\n", value);
> fclose(in);
> }
> ----[ end of code ]----
>
> the output, when running this, looks like:
>
> ki> ./double
> inbuf: 832220635 1980972567 2198208632
>
> 1980972567
> [0.000000]
>
> Using atof() instead of strtod() gives the same result, so the
> only thing I can think of is I'm missing something *really* simple here.
>
> If I change it to use atoi(), the value comes up correctly, but
> the second value in the inbuf comes out as:
>
> 2198208632
> [2147483647]
>
> Trying to set 'value' to be an unsigned int has the same result,
> as well as unsigned long/atol()...nothing wants to exceed the 2147483647
> barrier.
>
> So...is it something I'm missing? According to the Unix C reference
> I have, a 4byte int, unsigned, should give me 0->4294967295, but I'm only
> getting half of that.
>
> Marc G. Fournier scrappy@ki.net
> Systems Administrator @ ki.net scrappy@freebsd.org
>
>
----------------------------+-----------------------------------------------
Chuck Robey | Interests include any kind of voice or data
chuckr@eng.umd.edu | communications topic, C programming, and Unix.
9120 Edmonston Ct #302 |
Greenbelt, MD 20770 | I run Journey2 and n3lxx, both FreeBSD
(301) 220-2114 | version 2.2 current -- and great FUN!
----------------------------+-----------------------------------------------
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.OSF.3.91.960517012149.5735C-100000>
