Re: conversion of types.



CBFalconer wrote:
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.

[ snip ]

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



Relevant Pages

  • Re: Learning the fretboard..
    ... Sight reading at a higher level entails recognition of musical ... is your default handling of two whole steps along a string, ... -Start with a major triad, in root position, with the root on the ... There is only one way to play this triad. ...
    (rec.music.classical.guitar)
  • Re: Program slow. Garbage Collection
    ... the larger string I only had to build one string and call ToLoweron ... The three different possible approaches discussed so far -- "read the whole file", "read a line at a time as separate strings", "read a line at a time into a pre-allocated buffer" -- all have the bulk of their cost in the same thing: ... Reading a line at a time into separate strings does wind up creating a large number of individual strings. ... Also, these objects may survive long enough to wind up in the older generations of the heap; on the one hand, that alleviates the cost those objects cause for GC operations, but on the other hand you know for sure those objects are still going to get destroyed soon, so it'd be better for them to not wind up in the older generation where they may remain and cause increased memory pressure for a longer time. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Cross domain iframe access
    ... and WebKit do not allow to read back the `location' string value ... Such that any attempt to read the - toString - property, or if it can be read then to execute its value, may be the point at which a security exception is thrown. ... Other forms of direct access to the - location - object certainly can stand as a demonstration that reading its value is not subject to any security restrictions. ...
    (comp.lang.javascript)
  • Re: A Modern Method for Guitar
    ... as part of my new practice schedule, i'm reading for 30 minutes every ... Open position is actually much harder to play in than the upper positions because you have to deal with open strings ringing out when they're not supposed to be. ... I generally try to sight read in one of those positions and simply walk down the low E string for any notes below the position's range or up the high E string for nay notes above the position's range. ...
    (rec.music.makers.guitar.jazz)
  • Re: strange problem with serial communication
    ... Each time I send a string, ... > Your code is written to stop reading whenever ReadFile gets a timeout, ... > but you have set the timeout to zero. ... > only one byte for each ReadFile call. ...
    (microsoft.public.vc.mfc)