473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

conversion of types.

hello,
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 ?
thanks
------------
FILE *fp1;
char thing1[61];
double mtot;

fgets(thing1,61,fp1);
mtot = atof(thing1);

Aug 4 '06 #1
5 1602
jj_76 wrote:
hello,
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 ?
Pass successively a pointer to the beginning of each column to
strtod() and store the result in whatever way you want.

Spiros Bousbouras

Aug 4 '06 #2
jj_76 wrote:
hello,
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 ?
thanks
------------
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?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Aug 5 '06 #3
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!
Aug 6 '06 #4
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 ---
Aug 6 '06 #5
trm
jj_76 schrieb:
hello,
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 ?
thanks
------------
FILE *fp1;
char thing1[61];
double mtot;

fgets(thing1,61,fp1);
mtot = atof(thing1);
What did you expect it to do? atof() can only return a single value,
which you can store in a single variable, as you've shown. So it isn't
possible for it to convert more than one column.

Perhaps you were expecting it to "consume" the string, so that
multiple calls in sequence would convert the values of the other
columns? That just won't work. Consider that the parameter to atof()
is a string, that is, a (const char *). There simply isn't any way for
the function to alter the pointer so that the change is propagated
back up to the caller.

However, strtod() will do what you need. See CBFalconer's reply
to learn how it works.

Aug 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Vijai Kalyan | last post by:
Hello, I have come back to C++ after a couple of years with Java so I am quite rusty and this question may seem poor: My platform is Windows XP with MSVC 7.1. I have a class with a...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
2
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public...
9
by: Codemonkey | last post by:
Hi, Sorry for a stupid question, but is it possible to do a narrowing conversion with an object array with Option Strict On in VB? E.g: ------------------ Dim aBase as Base() = {New...
11
by: santosh | last post by:
Hello all, Conversion macros along the name of INT8_C, INT16_C etc, are defined in stdint.h to convert their argument into suitable representations for their corresponding types, i.e. int8_t,...
5
by: Pavils Jurjans | last post by:
Hello, I am somewhat lost in the implicit/expicit possible/impossible type casting in C#... I need to write a class, which among other things, must have wat to read a numeric value type, and...
7
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are...
8
by: Smithers | last post by:
Are there any important differences between the following two ways to convert to a type?... where 'important differences' means something more profound than a simple syntax preference of the...
8
by: Nikola | last post by:
Hello, I'm writing a String class for C++ and I'm getting the following error message when using operator: test.cpp: In function ‘int main()’: test.cpp:7: error: ISO C++ says that these are...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.