write a multidimensional array in a file

S

steph_de_marseille

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[0],array[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)
 
N

Netocrat

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[0],array[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[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.
 
S

steph_de_marseille

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.
 
N

Netocrat

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.
 
N

Netocrat

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...
 
B

Barry Schwarz

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[0],array[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[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>>
 
D

Dave Thompson

On 12 Jul 2005 23:51:49 -0700, (e-mail address removed) wrote:
fprintf(file1,"%f %f\ %f %f \n",array[0],array[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
 
F

Flash Gordon

Dave said:
On 12 Jul 2005 23:51:49 -0700, (e-mail address removed) wrote:
fprintf(file1,"%f %f\ %f %f \n",array[0],array[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.
 
D

Dave Thompson

On 12 Jul 2005 23:51:49 -0700, (e-mail address removed) wrote:
fprintf(file1,"%f %f\ %f %f \n",array[0],array[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
 

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,166
Messages
2,570,907
Members
47,448
Latest member
DeanaQ4445

Latest Threads

Top