copying 2D arrays to data file

R

Raj

Hi,

I want to copy an array A[3][900] to a data file. I know I need to
use fopen to first open the file and then use fwrite. It will be
great if someone could exactly tell me what the fwrite command would
be.
Thank you

Raj. R
 
R

Raj

Raj said:
I want to copy an array A[3][900] to a data file.  I know I need to
use fopen to first open the file and then use fwrite.  It will be
great if someone could exactly tell me what the fwrite command would
be.
Thank you

First off, 'fwrite' is not a command.  It's a function.  You will need
to call that function.  Second, how exactly you need to call it depends
on how you're going to *read* the information.  The reading and writing
have to be complementary.  If you don't know how you're going to read,
why write?

V

Hi,
I would like to read it in the form of 3 columns with 900 rows each.
Hence how would the function fwrite be? I saw that the function fwrite
has 4 parameters. The last is the pointer in which fopen is present.
I want to know what the other 3 parameters will be?
Thank you
Raj. R
 
A

Alf P. Steinbach

* Victor Bazarov:
Raj said:
I want to copy an array A[3][900] to a data file. I know I need to
use fopen to first open the file and then use fwrite. It will be
great if someone could exactly tell me what the fwrite command would
be.
Thank you

First off, 'fwrite' is not a command. It's a function. You will need
to call that function.

I'm sure that by "command" Raj means the fwrite routine invocation (or, in the
usual C/C++ terminology, function call). :)

So, Raj knows that the routine needs to be invoked.

And Raj is asking, what should that invocation look like?

I think the best response here is to point Raj towards the C++ higher level file
handling, in particular std::eek:fstream, like

std::eek:fstream file( filename );

Then the simplest is to loop through the array and for each element e,

file << e << " ";

Second, how exactly you need to call it depends
on how you're going to *read* the information. The reading and writing
have to be complementary.

Yep, and I think the best we can do is to recommend writing the data items as
textual specifications, which is what the '<<' operator does.

It's the most portable and generally the least troublesome.


Cheers,

- Alf
 
R

Raj

Alf said:
* Victor Bazarov:
Raj wrote:
I want to copy an array A[3][900] to a data file.  I know I need to
use fopen to first open the file and then use fwrite.  It will be
great if someone could exactly tell me what the fwrite command would
be.
Thank you
First off, 'fwrite' is not a command.  It's a function.  You will need
to call that function.
I'm sure that by "command" Raj means the fwrite routine invocation (or,
in the usual C/C++ terminology, function call). :)

Yes, an expression or a subexpression...  Right.
So, Raj knows that the routine needs to be invoked.

If you say so.
And Raj is asking, what should that invocation look like?

Perhaps.  And I think Raj needs to do some RTFMing.
I think the best response here is to point Raj towards the C++ higher
level file handling, in particular std::eek:fstream, like
  std::eek:fstream file( filename );
Then the simplest is to loop through the array and for each element e,
  file << e << " ";

"Best"?  Assuming that 'fopen' and 'fwrite' aren't the requirements of
the homework.
Yep, and I think the best we can do is to recommend writing the data
items as textual specifications, which is what the '<<' operator does.
It's the most portable and generally the least troublesome.

Portable, yes.  Best?  I don't know.  If the file is to be read by the
same program (presumably the same platform), then binary could actually
mean (a) faster/quicker reading and writing, (b) no loss of precision,
and (c) smaller files (in general).  If the file is not to be read by
the same program, then the format is dictated by the other program...

V

Hi,
Thanks for the immediate reply. I want to write it to an Ascii file
though its slower. Hence where should I look upon for the exact
function syntax which I need to include in my program.
Raj. R
 
J

James Kanze

Alf P. Steinbach wrote:

[...]
Yes. Before worrying about either, you really have to define
what the file should contain, in what format.

Agreed, but just saying you want a textual representation isn't
always enough. (Of course, we don't really know enough to say
more. Are the dimensions constant, or not, for example? Will
the file only contain a single array, or several?)
Portable, yes. Best? I don't know. If the file is to be
read by the same program (presumably the same platform), then
binary could actually mean (a) faster/quicker reading and
writing, (b) no loss of precision, and (c) smaller files (in
general).

If the file is to be read by the same executable (not the same
program, recompiled with a new version of the compiler or with
different options), and if the program is guaranteed to work
first time, and never need to be debugged, then yes, there's no
problem with binary, and it could be a little bit faster. If
the application really isn't fast enough otherwise, a binary
format could be considered.
If the file is not to be read by the same program, then the
format is dictated by the other program...

All of the programs accessing the file have to agree on the
format. Which is why it is defined before you write any of the
programs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,431
Latest member
ElyseG3173

Latest Threads

Top