469,640 Members | 1,579 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,640 developers. It's quick & easy.

write a multidimensional array in a file

I would like to write a [250]x[250] array in a file.

If the array has a small numbers of columns I know I can use a loop
like:

int i,j;
double array[50][2];
FILE *file1;
file1=fopen("data.dat","w");
for(i=0;i<50;i++){
fprintf(file1,"%f %f\ %f %f \n",array[i][0],array[i][1]);
}
}
fclose(file1);

but in a case a 250 (for example) columns????
I guess there is another way than writing 250 times array[][]???

I did it in fortran

open(unit=1,file='tab4')
do 102 k=1,2*nrow+1
write(unit=1,fmt=200)(Y(k,i),i=1,ncol)
102 continue
200 format(2X,200F10.4)
close(1)

Nov 15 '05 #1
9 4131
On Tue, 12 Jul 2005 23:51:49 -0700, steph_de_marseille wrote:
I would like to write a [250]x[250] array in a file.
*note that you say this but your demonstration code uses an array of size
[50][2] so that's what I've used.

If the file will be written and read back on the same machine (ie you
don't need to worry about portability), and it doesn't need to be humanly
readable, you can open the file in binary mode and read/write the whole
array as it is represented in memory to the file in one operation. In this
case use "wb" rather than "w".

if (! fwrite(array, sizeof(array), 1, file1)) {
perror("fwrite");
exit(EXIT_FAILURE);
}

Then to get back the data, you can declare:
double (*arrayp)[2] = malloc(sizeof(*arrayp) * 50);
if (!arrayp) {
perror("malloc");
exit(EXIT_FAILURE);
}

Open the file in "rb" mode, and use:
if (! fread(arrayp, sizeof(*arrayp) * 50, 1, file1)) {
perror("fread");
exit(EXIT_FAILURE);
}

Then to access elements use arrayp[x][y] as usual.
If the array has a small numbers of columns I know I can use a loop like:

int i,j;
double array[50][2];
FILE *file1;
file1=fopen("data.dat","w");
You should check for fopen returning an error.
for(i=0;i<50;i++){
fprintf(file1,"%f %f\ %f %f \n",array[i][0],array[i][1]);
}
This fprintf statement has a few problems: firstly you have 4 %f's and
only two double arguments. The "\ " doesn't seem to serve any purpose.
Also you aren't checking for fprintf returning an error.

Finally you may or may not want to limit the number of digits being
printed if it is for human reading and precision is not an issue. In this
case use %10.4f or something like it, where the 10 is the maximum total
number of digits and the 4 is the maximum number of digits after the
decimal point.

If you simply want to print the array to the file in a portable, humanly
readable format, then this double loop may serve your needs. The only
minor annoyance is that there will be a trailing space at the end of each
line, but you can eliminate that with a little extra code.

for (i = 0; i < 50; i++) {
for (j = 0; j < 2; j++) {
if (fprintf(file1, "%f ", array[i][j]) < 0) {
perror("fprintf");
exit(EXIT_FAILURE);
}
}
if (fprintf(file1, "\n") < 0) {
perror("fprintf");
exit(EXIT_FAILURE);
}
}

You can read this file back into an array but I won't post code for that
since it's a little more involved and you haven't said that you need to do
that anyway.
fclose(file1);
Again, check for fclose returning an error.
but in a case a 250 (for example) columns???? I guess there is another
way than writing 250 times array[][]???

I did it in fortran

open(unit=1,file='tab4')
do 102 k=1,2*nrow+1
write(unit=1,fmt=200)(Y(k,i),i=1,ncol)
102 continue
200 format(2X,200F10.4)
close(1)


If I understood Fortran I would know better what you are trying to
achieve; but it looks to me like this will produce a humanly readable
file. Anyhow that gives you two options.

Nov 15 '05 #2
Thanks Netocrat for your answer,

In fact, in the case of the 250X250 array, I wrote a "not humanly
readable" binary file, with 2 columns and 250x250lines, but i would
like to have a "humanly readable" file with 250 lines and 250 columns,
which looks like my array.

Nov 15 '05 #3
On Wed, 13 Jul 2005 01:58:23 -0700, steph_de_marseille wrote:
Thanks Netocrat for your answer,

In fact, in the case of the 250X250 array, I wrote a "not humanly
readable" binary file, with 2 columns and 250x250lines, but i would like
to have a "humanly readable" file with 250 lines and 250 columns, which
looks like my array.


The loops that I posted will do this if you change the numbers from 50 and
2 to 250. I don't know how readable the file will be with 250 numbers on
one line though.

Nov 15 '05 #4
On Wed, 13 Jul 2005 19:20:40 +1000, Netocrat wrote:
On Wed, 13 Jul 2005 01:58:23 -0700, steph_de_marseille wrote:
Thanks Netocrat for your answer,

In fact, in the case of the 250X250 array, I wrote a "not humanly
readable" binary file, with 2 columns and 250x250lines, but i would like
to have a "humanly readable" file with 250 lines and 250 columns, which
looks like my array.


The loops that I posted will do this if you change the numbers from 50 and
2 to 250. I don't know how readable the file will be with 250 numbers on
one line though.


Troll suckered me even as I was cluing on...
Nov 15 '05 #5
On 12 Jul 2005 23:51:49 -0700, st****************@yahoo.fr wrote:
I would like to write a [250]x[250] array in a file.

If the array has a small numbers of columns I know I can use a loop
like:

int i,j;
double array[50][2];
FILE *file1;
file1=fopen("data.dat","w");
for(i=0;i<50;i++){
You would replace the limit in this with 250.
fprintf(file1,"%f %f\ %f %f \n",array[i][0],array[i][1]);
This contains a syntax error and invokes undefined behavior.
Backslash-blank is not a valid escape sequence like \n. You have four
format specifiers but only two arguments t6o satisfy them.

Getting back to your real question, you would replace the single call
to fprintf with a loop like the following

for (j = 0; j < 250; j++)
fprintf(file1,"%f ", array[i][j]);

and then add a final (for this line) call to printf to terminate the
line

fprintf(file1,"\n");

which will execute once per line after all the variables for that line
have been written.
}
}
fclose(file1);

but in a case a 250 (for example) columns????
I guess there is another way than writing 250 times array[][]???

I did it in fortran

open(unit=1,file='tab4')
do 102 k=1,2*nrow+1
write(unit=1,fmt=200)(Y(k,i),i=1,ncol)
102 continue
200 format(2X,200F10.4)
close(1)


<<Remove the del for email>>
Nov 15 '05 #6
thanks a lot to both of you!

Nov 15 '05 #7
On Wed, 13 Jul 2005 13:48:26 -0700, Barry Schwarz <sc******@deloz.net>
wrote:
On 12 Jul 2005 23:51:49 -0700, st****************@yahoo.fr wrote:

<snip>
fprintf(file1,"%f %f\ %f %f \n",array[i][0],array[i][1]);


This contains a syntax error and invokes undefined behavior.
Backslash-blank is not a valid escape sequence like \n. You have four
format specifiers but only two arguments t6o satisfy them.


Nit: the (or any) syntax error also invokes UB, but if this phrasing
was meant to say that the extra arguments to fprintf() cause UB,
that's wrong. They're safely discarded (7.19.6.1p2) just as they are
for other (user) vararg functions. Unless of course the arguments
cause UB within themselves, like two assignments to same object.

- David.Thompson1 at worldnet.att.net
Nov 15 '05 #8
Dave Thompson wrote:
On Wed, 13 Jul 2005 13:48:26 -0700, Barry Schwarz <sc******@deloz.net>
wrote:

On 12 Jul 2005 23:51:49 -0700, st****************@yahoo.fr wrote:


<snip>
fprintf(file1,"%f %f\ %f %f \n",array[i][0],array[i][1]);


This contains a syntax error and invokes undefined behavior.
Backslash-blank is not a valid escape sequence like \n. You have four
format specifiers but only two arguments t6o satisfy them.

Nit: the (or any) syntax error also invokes UB, but if this phrasing
was meant to say that the extra arguments to fprintf() cause UB,
that's wrong. They're safely discarded (7.19.6.1p2) just as they are
for other (user) vararg functions. Unless of course the arguments
cause UB within themselves, like two assignments to same object.


You misread the above. The format specifier says there are 4 doubles,
but only two actual arguments have been provided, leaving to instances
of %f without corresponding parameters.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #9
On Mon, 18 Jul 2005 02:21:50 GMT, I wrote:
On Wed, 13 Jul 2005 13:48:26 -0700, Barry Schwarz <sc******@deloz.net>
wrote:
On 12 Jul 2005 23:51:49 -0700, st****************@yahoo.fr wrote:

<snip>
fprintf(file1,"%f %f\ %f %f \n",array[i][0],array[i][1]);


This contains a syntax error and invokes undefined behavior.
Backslash-blank is not a valid escape sequence like \n. You have four
format specifiers but only two arguments t6o satisfy them.


Nit: the (or any) syntax error also invokes UB, but if this phrasing
was meant to say that the extra arguments to fprintf() cause UB,
that's wrong. They're safely discarded (7.19.6.1p2) just as they are
for other (user) vararg functions. Unless of course the arguments
cause UB within themselves, like two assignments to same object.

AARGH! My brain apparently ran backwards temporarily. Sorry.
This is (would be) missing not extra arguments and is indeed UB.
- David.Thompson1 at worldnet.att.net
Nov 15 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by janetb | last post: by
1 post views Thread by Mark Smith | last post: by
2 posts views Thread by chris | last post: by
2 posts views Thread by xhunga | last post: by
1 post views Thread by Chuy08 | last post: by
5 posts views Thread by LittleCake | last post: by
reply views Thread by Szabolcs Borsanyi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.