From owner-freebsd-hackers Thu May 16 22:22:50 1996 Return-Path: owner-hackers Received: (from root@localhost) by freefall.freebsd.org (8.7.3/8.7.3) id WAA13651 for hackers-outgoing; Thu, 16 May 1996 22:22:50 -0700 (PDT) Received: from distortion.eng.umd.edu (distortion.eng.umd.edu [129.2.98.6]) by freefall.freebsd.org (8.7.3/8.7.3) with ESMTP id WAA13646 for ; Thu, 16 May 1996 22:22:48 -0700 (PDT) Received: from gilligan.eng.umd.edu (gilligan.eng.umd.edu [129.2.98.205]) by distortion.eng.umd.edu (8.7.5/8.7.3) with ESMTP id BAA07313; Fri, 17 May 1996 01:22:46 -0400 (EDT) Received: (from chuckr@localhost) by gilligan.eng.umd.edu (8.7.5/8.7.3) id BAA05801; Fri, 17 May 1996 01:22:46 -0400 (EDT) Date: Fri, 17 May 1996 01:22:45 -0400 (EDT) From: Chuck Robey X-Sender: chuckr@gilligan.eng.umd.edu To: "Marc G. Fournier" cc: hackers@freebsd.org Subject: Re: C Programming Question In-Reply-To: Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-hackers@freebsd.org X-Loop: FreeBSD.org Precedence: bulk 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 > #include > > #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! ----------------------------+-----------------------------------------------