writing data to file

R

Raj

Hi,
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
Raj. R

--------------------------------------------------------------------------------------------
float xcentr1,ycentr1,zcentr1;
float *ffpoint=NULL;
int n,i;
float ff[3][1000];
FILE *centro;
centro=fopen("cx.txt","w");
ffpoint=*ff;
ff[1][n]=xcentr1;
ff[2][n]=ycentr1;
ff[3][n]=zcentr1;
------- /* Get OUTPUTS TO DATA FILE*/-----------------
for(i=0;i<3;i++){
fwrite(ffpoint,1,sizeof(ffpoint),centro);
fclose("cx.txt");}

------------------------------------------------------------------------------
 
D

Default User

Raj said:
Hi,
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
Raj. R
float *ffpoint=NULL;
int n,i;
float ff[3][1000];
FILE *centro;
centro=fopen("cx.txt","w");
ffpoint=*ff;
ff[1][n]=xcentr1;
ff[2][n]=ycentr1;
ff[3][n]=zcentr1;
------- /* Get OUTPUTS TO DATA FILE*/-----------------
for(i=0;i<3;i++){
fwrite(ffpoint,1,sizeof(ffpoint),centro);
fclose("cx.txt");}

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.





Brian
 
R

Raj

Hi,
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?
Thank you once again

Raj. R


Raj said:
Hi,
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
Raj. R
  float *ffpoint=NULL;
  int  n,i;
  float ff[3][1000];
  FILE *centro;
  centro=fopen("cx.txt","w");
  ffpoint=*ff;
   ff[1][n]=xcentr1;
   ff[2][n]=ycentr1;
   ff[3][n]=zcentr1;
-------    /* Get OUTPUTS TO DATA FILE*/-----------------
   for(i=0;i<3;i++){
   fwrite(ffpoint,1,sizeof(ffpoint),centro);
   fclose("cx.txt");}

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.

Brian- Hide quoted text -

- Show quoted text -
 
R

Raj

Hi,
One more additional thing, I am using Visual Studio 08 for compiling
the C program. I haven't included the iostream.h in the program as
there is no iostream header file in the library. (in the same folder
as stdio.h)
Thank you
Raj. R
 
R

Raj

Hi,
Thanks for the reply. I had a look at the folder and I could see
iostream. In the tutorials in the web I referred, I couldn't
understand how to write arrays into files using fprintf. I just could
understand statements being written into a file.
Raj. R
 
M

Michael DOUBEZ

Raj said:
Hi,
One more additional thing, I am using Visual Studio 08 for compiling
the C program. I haven't included the iostream.h

You shoud however include said:
in the program as
there is no iostream header file in the library. (in the same folder
as stdio.h)

Did you have a look in "Program Files\Microsoft Visual
Studio\VC98\Include" ?
(Path may change a bit).

For writing to a file using iostream or fprintf(), please refer to one
of the numerous tutorial on the web.
 
M

Michael DOUBEZ

Raj said:
Hi,
Thanks for the reply. I had a look at the folder and I could see
iostream. In the tutorials in the web I referred, I couldn't
understand how to write arrays into files using fprintf. I just could
understand statements being written into a file.

Have a look at the following example:
http://www.cplusplus.com/reference/clibrary/cstdio/fprintf.html

IMHO it would be best if you read a book about programming with c++.
Accelerated c++ is a good choice:
http://www.acceleratedcpp.com/
 
D

Default User

Raj wrote:

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>

Post rearranged.
Hi,
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?
Thank you once again


The printf() function is something that should be in a C text. 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.

An example use of fprintf() is:

double val = 1.0/3.0;

fprintf(f, "%.3f\n", val);


What this does is convert the binary floating point value to a string
representation in decimal and writes it to the file. This assumes f is
a previously opened text file. It truncates the decimal points at three
and adds a newline after, so in the file you will see:

..333

Is that what you want?


iostream can do the same sort of thing, only using different methods.
Any C++ book should cover how to do that. From a follow-up message, it
seems like you don't have a modern book. You might need to get a more
updated one.




Brian
 
R

Raj

Raj wrote:

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>

Post rearranged.






The printf() function is something that should be in a C text. 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.

An example use of fprintf() is:

double val = 1.0/3.0;

fprintf(f, "%.3f\n", val);

What this does is convert the binary floating point value to a string
representation in decimal and writes it to the file. This assumes f is
a previously opened text file. It truncates the decimal points at three
and adds a newline after, so in the file you will see:

.333

Is that what you want?

iostream can do the same sort of thing, only using different methods.
Any C++ book should cover how to do that. From a follow-up message, it
seems like you don't have a modern book. You might need to get a more
updated one.

Brian

Hi

Thanks for the website. I presumed for writing Arrays to a text file
in ASCII format, I will just include the fprintf command in the for
loop. where ff is a 2D array ff[3][10] Thank you
centro=fopen("cx.txt","w");
ffpoint=*ff;
for(n=0;n<10;n++){
for(i=0;i<3;i++){
fprintf(centro,ffpoint);
}
Raj . R
 
D

Default User

Thanks for the website. I presumed for writing Arrays to a text file
in ASCII format, I will just include the fprintf command in the for
loop. where ff is a 2D array ff[3][10] Thank you
centro=fopen("cx.txt","w");
ffpoint=*ff;
for(n=0;n<10;n++){
for(i=0;i<3;i++){
fprintf(centro,ffpoint);
}

What you have is not correct. Something along the lines of:

for(n=0;n<3;n++){
for(i=0;i<10;i++){
fprintf(centro,"%.3f\n", ffpoint[n]);
}
}

Might be. That would iterate over each subarray, printing each element
on a separate line in the file. If you wanted something like all the
values of a row separated by space or tab, with each row set off with a
newline, then these would be minor changes to the algorithm.




Brian
 
J

James Kanze

Raj wrote:
You shoud however include <iostream> not <iostream.h>.
Did you have a look in "Program Files\Microsoft Visual
Studio\VC98\Include" ? (Path may change a bit).

Just a nit, but you really shouldn't bother. Visual Studio is a
modern C++ compiler; he's writing new code. He should just
include <iostream>, and be done with it; if that doesn't work
(but it will), then you can forget about using the compiler for
any serious C++ work. (Normally, I would expect <iostream.h> to
work as well. But only to support legacy code, since it worked
in earlier versions of VC++. Whether it works or not is
completely irrelevant for new code.)
 
J

James Kanze

Raj wrote:
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::eek:stream_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.
 
B

Balog Pal

Just a nit, but you really shouldn't bother. Visual Studio is a
modern C++ compiler; he's writing new code. He should just
include <iostream>, and be done with it; if that doesn't work
(but it will), then you can forget about using the compiler for
any serious C++ work. (Normally, I would expect <iostream.h> to
work as well. But only to support legacy code, since it worked
in earlier versions of VC++. Whether it works or not is
completely irrelevant for new code.)
<<

VS2008 does not have iostream.h
 

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,427
Latest member
HildredDic

Latest Threads

Top