From: scooper on
Would anyone please be able to tell me how to read and write a float or
double to a text file.
I am stuck on reading a line from the file into a string variable but cannot
use the atof or strtod functions because it isn't a char[] array variable
and a char array variable can't read a line from a file(?).
What do I do?
scooper


From: osmium on
"scooper" wrote:

> Would anyone please be able to tell me how to read and write a float or
> double to a text file.
> I am stuck on reading a line from the file into a string variable but
> cannot use the atof or strtod functions because it isn't a char[] array
> variable and a char array variable can't read a line from a file(?).
> What do I do?

You don't mention a language, C or C++. For C, you can convert a double
into a string with the sprintf() function in <stdio.h>, then write the
resulting string to a text file.

For C++, there are better ways.


From: Richard Heathfield on
In <00aea297$0$15603$c3e8da3(a)news.astraweb.com>, scooper wrote:

> Would anyone please be able to tell me how to read and write a float
> or double to a text file.
> I am stuck on reading a line from the file into a string variable
> but cannot use the atof or strtod functions because it isn't a
> char[] array variable and a char array variable can't read a line
> from a file(?). What do I do?

#include <stdio.h>

int main(int argc, char **argv)
{
double d = 3.1415926;
float f = 42.01F;

FILE *fp = stdout;
if(argc > 1)
{
fp = fopen(argv[1], "w");
}

if(fp != NULL)
{
fprintf(fp, "%f %f\n", d, f);
}

if(fp != NULL && fp != stdout)
{
fclose(fp);
}
return 0;
}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
From: Richard Heathfield on
In <fopen-20091214031708(a)ram.dialup.fu-berlin.de>, Stefan Ram wrote:

> Richard Heathfield <rjh(a)see.sig.invalid> writes:
>> if(fp != NULL)
>> {
>> fprintf(fp, "%f %f\n", d, f);
>> }
>>
>> if(fp != NULL && fp != stdout)
>
> Seing fp being tested twice made me nervous, so I rewrote it:
>
> #include <stdio.h>
>
> void printto( FILE * const fp )
> { double const d = 3.1415926;
> float const f = 42.01F;
> fprintf( fp, "%f %f\n", d, f ); }
>
> int main( int argc, char * argv[] )
> { if( argc > 1 )
> { FILE * const fp = fopen( argv[ 1 ], "w" );
> if( fp ){ printto( fp ); fclose( fp ); }}
> else printto( stdout ); }

Seeing printto called twice made /me/ nervous! This is one of those
situations where you have to duplicate /something/ (I duplicated the
fp test, and you duplicated the print functionality, albeit very
wisely encapsulating it), and either choice can be shown to be
superior in some way to the other choice - but you only get to choose
one. That's one of the subtler skills of programming - making the
right choice among several valid but mutually exclusive
possibilities.

Of course, this is all incidental to the OP's original query, which
has now been thoroughly answered in C. A C++ solution would perhaps
not go amiss, but I'm not the best person to give that solution - I'd
just use std::cout and be done with it, but maybe there's a better
way.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
From: Alf P. Steinbach on
* Richard Heathfield:
> In <fopen-20091214031708(a)ram.dialup.fu-berlin.de>, Stefan Ram wrote:
>
>> Richard Heathfield <rjh(a)see.sig.invalid> writes:
>>> if(fp != NULL)
>>> {
>>> fprintf(fp, "%f %f\n", d, f);
>>> }
>>>
>>> if(fp != NULL && fp != stdout)
>> Seing fp being tested twice made me nervous, so I rewrote it:
>>
>> #include <stdio.h>
>>
>> void printto( FILE * const fp )
>> { double const d = 3.1415926;
>> float const f = 42.01F;
>> fprintf( fp, "%f %f\n", d, f ); }
>>
>> int main( int argc, char * argv[] )
>> { if( argc > 1 )
>> { FILE * const fp = fopen( argv[ 1 ], "w" );
>> if( fp ){ printto( fp ); fclose( fp ); }}
>> else printto( stdout ); }
>
> Seeing printto called twice made /me/ nervous! This is one of those
> situations where you have to duplicate /something/ (I duplicated the
> fp test, and you duplicated the print functionality, albeit very
> wisely encapsulating it), and either choice can be shown to be
> superior in some way to the other choice - but you only get to choose
> one. That's one of the subtler skills of programming - making the
> right choice among several valid but mutually exclusive
> possibilities.
>
> Of course, this is all incidental to the OP's original query, which
> has now been thoroughly answered in C. A C++ solution would perhaps
> not go amiss, but I'm not the best person to give that solution - I'd
> just use std::cout and be done with it, but maybe there's a better
> way.


#include <stdio.h>
#include <stdlib.h>

int main( int argc, char **argv )
{
double const d = 3.1415926;
float const f = 42.01F;

FILE* const fp = (argc > 1? fopen( argv[1], "w" ) : stdout);

if( fp == 0 )
{
return EXIT_FAILURE;
}

fprintf( fp, "%f %f\n", d, f );

if( fp != stdout )
{
fclose( fp );
}
}


Cheers,

- Alf