Joe Wright wrote:
jj_76 wrote:
>I have a data file with several columns consisting of double
values. I am reading the whole row as a string using fgets().
I want to convert each column back into a double. It seems atof()
converts only the first column and strtod() also does the same.
Is there any other way ?
FILE *fp1;
char thing1[61];
double mtot;
fgets(thing1,61,fp1);
mtot = atof(thing1);
What is the actual format of the file? Are there a fixed number
of columns (fields) per row? How are the fields delimited?
What does that matter, given his description as a set of columns?
The description of strtod() (below, from N869) should suffice for
the OP, since the returned endptr will always describe from where
to extract the next value.
7.20.1.3 The strtod, strtof, and strtold functions
Synopsis
[#1]
#include <stdlib.h>
double strtod(const char * restrict nptr,
char ** restrict endptr);
float strtof(const char * restrict nptr,
char ** restrict endptr);
long double strtold(const char * restrict nptr,
char ** restrict endptr);
Description
[#2] The strtod, strtof, and strtold functions convert the
initial portion of the string pointed to by nptr to double,
float, and long double representation, respectively. First,
they decompose the input string into three parts: an
initial, possibly empty, sequence of white-space characters
(as specified by the isspace function), a subject sequence
resembling a floating-point constant or representing an
infinity or NaN; and a final string of one or more
unrecognized characters, including the terminating null
character of the input string. Then, they attempt to
convert the subject sequence to a floating-point number, and
return the result.
--
Chuck F (cb********@yahoo.com) (cb********@maineline.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.netUSE maineline address!