Re: writing data to file
On Mar 19, 4:56 pm, "Default User" <defaultuse...@yahoo.com> wrote:
Raj wrote:
On Mar 18, 7:25 pm, "Default User" <defaultuse...@yahoo.com> wrote:
Raj wrote:
I would like to copy an array into a data file (ASCII)
format. I would like to know if the following syntax is
correct. ff is the required array i want to be written
in the data file. Thank you
Probably not. If you are expecting to see a text file with
floating point numbers in human readable form, then that's
not what you'll get. fwrite() is designed with a binary
file, and would write the byte representation of your
floats to the file.
If that's what you want, then open it as a binary. If you
want a text file, use fprintf() or the C++ iostream
operators.
Thanks for the immediate reply. So how will fprintf be used
to write an array? and Could you please let me know more
about the iostream operators too?
The printf() function is something that should be in a C text.
And only in a C program. It's an example of how not to do
things.
There are also a few good online references. To use it or the
iostream operator, you aren't going to be able to write out an
array of numbers all at once, as you could with fwrite().
You'll have to deal with each value individually.
First, you can't do it with fwrite anyway; fwrite really only
works with preformatted data, not with raw data. But with
iostream's, you can do things like:
std::copy( matrix.begin(), matrix.end(),
std::ostream_iterator< double >( out, " " ) ;
All you need is that matrix provide an appropriate iterator.
(And of course, you have to set up the desired format and
precision beforehand.) Whether this is appropriate or not is
another question. In most cases, I would expect the matrix
class to provide an appropriate operator <<, so you'd just
write:
out << matrix ;
and be done with it.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34