Re: conversion of types.
- From: Joe Wright <joewwright@xxxxxxxxxxx>
- Date: Sun, 06 Aug 2006 13:29:40 -0400
CBFalconer wrote:
Joe Wright wrote:[ snip ]jj_76 wrote:
I have a data file with several columns consisting of doubleWhat is the actual format of the file? Are there a fixed number
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);
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.
Hi Chuck, welcome back.
What does the file format matter? You cannot make sense of a file unless you know exactly how it was written. I might write doubles to a binary file where each double is eight bytes long. For you to read that file successfully you'd have to know that. Such things as endianness become important. Certainly fgets() is useless on a binary file of doubles.
So now, assuming a text file and fprintf() we might write two doubles, 3 and 4..
3.000000
4.000000
...which are essentially concatenated to "3.0000004.000000" and so on. What is strtod() and company to do with this? Nothing rational. You need to delimit the values with an 'out-of-bound' character like '|' or something. If the above string were delimited like..
"3.000000|4.000000|"
...then we know what to do.
You have to know the format of the file you are reading.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
.
- References:
- conversion of types.
- From: jj_76
- Re: conversion of types.
- From: Joe Wright
- Re: conversion of types.
- From: CBFalconer
- conversion of types.
- Prev by Date: Re: indentify if argument is char or float
- Next by Date: Re: indentify if argument is char or float
- Previous by thread: Re: conversion of types.
- Next by thread: Re: conversion of types.
- Index(es):
Relevant Pages
|