Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 16 May 1996 14:56:36 -0400 (EDT)
From:      "Marc G. Fournier" <scrappy@ki.net>
To:        hackers@freebsd.org
Subject:   C Programming Question 
Message-ID:  <Pine.NEB.3.93.960516144301.7805B-100000@freebsd.ki.net>

next in thread | raw e-mail | index | archive | help

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:

---[ 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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.NEB.3.93.960516144301.7805B-100000>