write 2D array to binary file

B

Betty Hickman

I'm having trouble writing a 2D array to a binary file. Here's what I
have:

FILE *outfile;
short **clump_class;

clump_class = malloc(nrows * sizeof(short *));
for (i=0; i<nrows; ++i)
clump_class = calloc (ncolumns, sizeof(short *));

/* other stuff to put data into clump_class */

if ((outfile = fopen(argv[2],"wb")) == NULL)
{
printf("Unable to open output file %s\n",argv[2]);
exit(0);
}

for (i=0; i<nrows; ++i)
fwrite(clump_class,sizeof(short),ncolumns,outfile);

Since nrows = ncolumns = 510, if this worked, it should produce a file
with 510*510*2 = 520200 bytes. However, I get a file with 507904
bytes and when I try to read it in with another program, it's not what
I expect.

I also tried:

for (i=0; i<nrows; ++i)
fwrite(clump_class,ncolumns*sizeof(short),1,outfile);

and

for (i=0; i<nrows; ++i)
for (j=0; j<ncolumns; ++j)
fwrite(&clump_class[j],sizeof(short),1,outfile);

but neither works correctly.

I'm obviously missing something here, so any pointers would be greatly
appreciated.

Thanks!
 
E

Eric Sosman

Betty said:
I'm having trouble writing a 2D array to a binary file. Here's what I
have:

FILE *outfile;
short **clump_class;

clump_class = malloc(nrows * sizeof(short *));
for (i=0; i<nrows; ++i)
clump_class = calloc (ncolumns, sizeof(short *));


Here's one problem, which may or may not be THE
problem: you surely mean `sizeof(short)' rather than
`sizeof(short*)'. The c.l.c. preferred style

clump_class = malloc(nrows * sizeof *clump_class);
...
clump_class = calloc(ncolumns, sizeof *clump_class);

.... makes this class of error hard to commit.

On the face of it, though, this is unlikely to
explain the particular symptom you're seeing. Still,
when dealing with a baffling error it's always a good
idea to fix all the known errors first, even if they
appear to be unrelated.

The only other suggestion I can offer is to check
the value returned by the fwrite() calls. One or more
may be failing, and it would be good to know that ...
 
M

Mike Wahler

Betty Hickman said:
I'm having trouble writing a 2D array to a binary file. Here's what I
have:

FILE *outfile;
short **clump_class;

clump_class = malloc(nrows * sizeof(short *));
for (i=0; i<nrows; ++i)
clump_class = calloc (ncolumns, sizeof(short *));

/* other stuff to put data into clump_class */

if ((outfile = fopen(argv[2],"wb")) == NULL)
{
printf("Unable to open output file %s\n",argv[2]);
exit(0);
}

for (i=0; i<nrows; ++i)
fwrite(clump_class,sizeof(short),ncolumns,outfile);

Since nrows = ncolumns = 510, if this worked, it should produce a file
with 510*510*2 = 520200 bytes. However, I get a file with 507904
bytes and when I try to read it in with another program, it's not what
I expect.

I also tried:

for (i=0; i<nrows; ++i)
fwrite(clump_class,ncolumns*sizeof(short),1,outfile);

and

for (i=0; i<nrows; ++i)
for (j=0; j<ncolumns; ++j)
fwrite(&clump_class[j],sizeof(short),1,outfile);

but neither works correctly.

I'm obviously missing something here, so any pointers would be greatly
appreciated.


#include <stdio.h>
#include <stdlib.h>

/* returns address of allocated array, returns NULL on failure */
int **create(size_t rows, size_t cols)
{
size_t row = 0;
size_t tmp = 0;

int **arr = malloc(rows * sizeof *arr);

if(arr)
for(row = 0; row < rows; ++row)
if(!(arr[row] = calloc(cols, sizeof **arr)))
{
for(tmp = 0; tmp < row; ++tmp)
free(arr[tmp]);

free(arr);
arr = 0;
break;
}

return arr;

}

/* returns 0 for success, nonzero for failure */
int write(FILE *fp, int **arr, size_t rows, size_t cols)
{
size_t row = 0;
size_t col = 0;

for(row = 0; row < rows; ++row)
if(fwrite(arr[row], sizeof **arr, cols, fp) < cols)
break;

return row < rows;
}

/* store test data in array */
void store(int **arr, size_t rows, size_t cols)
{
size_t row = 0;
size_t col = 0;

for(row = 0; row < rows; ++row)
{
for(col = 0; col < cols; ++col)
arr[row][col] = row * cols + col;
}
}

/* display array element values */
void show(int **arr, size_t rows, size_t cols)
{
size_t row = 0;
size_t col = 0;

for(row = 0; row < rows; ++row)
{
for(col = 0; col < cols; ++col)
printf("%3d", arr[row][col]);

putchar('\n');
}

}

int main()
{
const size_t rows = 5;
const size_t cols = 10;

FILE *f = 0;
int **array = create(rows, cols);

if(!array)
{
fprintf(stderr, "Cannot allocate memory\n");
return EXIT_FAILURE;
}

store(array, rows, cols);
show(array, rows, cols);

f = fopen("data", "wb");

if(!f)
{
fprintf(stderr, "Cannot open file for writing\n");
free(array);
return EXIT_FAILURE;
}

if(write(f, array, rows, cols))
fprintf(stderr, "Error writing to file\n");

if(fclose(f))
fprintf(stderr, "Error closing file\n");

free(array);
return 0;
}

-Mike
 
J

Joe Wright

Betty said:
I'm having trouble writing a 2D array to a binary file. Here's what I
have:

FILE *outfile;
short **clump_class;

clump_class = malloc(nrows * sizeof(short *));
for (i=0; i<nrows; ++i)
clump_class = calloc (ncolumns, sizeof(short *));

/* other stuff to put data into clump_class */

if ((outfile = fopen(argv[2],"wb")) == NULL)
{
printf("Unable to open output file %s\n",argv[2]);
exit(0);
}

for (i=0; i<nrows; ++i)
fwrite(clump_class,sizeof(short),ncolumns,outfile);

Since nrows = ncolumns = 510, if this worked, it should produce a file
with 510*510*2 = 520200 bytes. However, I get a file with 507904
bytes and when I try to read it in with another program, it's not what
I expect.

I also tried:

for (i=0; i<nrows; ++i)
fwrite(clump_class,ncolumns*sizeof(short),1,outfile);

and

for (i=0; i<nrows; ++i)
for (j=0; j<ncolumns; ++j)
fwrite(&clump_class[j],sizeof(short),1,outfile);

but neither works correctly.

I'm obviously missing something here, so any pointers would be greatly
appreciated.

Thanks!


You didn't give us a real program. Here's one.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
FILE *outfile;
short **clump_class;
int nrows = 510, ncols = 510;
int i;

clump_class = malloc(nrows * sizeof *clump_class);

for (i = 0; i < nrows; ++i)
*(clump_class + i) = calloc(ncols, sizeof **clump_class);

outfile = fopen("betty.bin", "wb");

for (i = 0; i < nrows; ++i)
fwrite(*(clump_class + i), sizeof **clump_class, ncols, outfile);

fclose(outfile);

return 0;
}

This program writes 520,200 bytes to betty.bin. I can't tell why
yours doesn't because you didn't post a real program.
 
H

Herbert Rosenau

I'm having trouble writing a 2D array to a binary file. Here's what I
have:

FILE *outfile;
short **clump_class;

clump_class = malloc(nrows * sizeof(short *));
for (i=0; i<nrows; ++i)
clump_class = calloc (ncolumns, sizeof(short *));

/* other stuff to put data into clump_class */

if ((outfile = fopen(argv[2],"wb")) == NULL)
{
printf("Unable to open output file %s\n",argv[2]);
exit(0);
}

for (i=0; i<nrows; ++i)
fwrite(clump_class,sizeof(short),ncolumns,outfile);

Since nrows = ncolumns = 510, if this worked, it should produce a file
with 510*510*2 = 520200 bytes. However, I get a file with 507904
bytes and when I try to read it in with another program, it's not what
I expect.

I also tried:

for (i=0; i<nrows; ++i)
fwrite(clump_class,ncolumns*sizeof(short),1,outfile);

and

for (i=0; i<nrows; ++i)
for (j=0; j<ncolumns; ++j)
fwrite(&clump_class[j],sizeof(short),1,outfile);

but neither works correctly.

I'm obviously missing something here, so any pointers would be greatly
appreciated.

Thanks!


You don't create a 2 dim. array. You creates an array of pointers to
arrays of shorts.

In that case you should write/read eaxch array of short separately
to/from file.

to create a 2 dim array you would use

short *p = malloc(nrows * ncols * sizeof(*p)). Then you have a real 2
dimensional array that you can address like

p[row][col]

and handle as whole in read/write operations.
 
B

Betty Hickman

Sorry, I guess I should have posted the entire program, but I thought
I had included all the pertinent info. Turns out that my problem was
in my close statement---I had the wrong file name (I had copied an
earlier close statement with an input file name and failed to change
it. Anyway, the output file was not closed normally, so the buffer
apparently didn't get flushed and I ended up with fewer bytes than
intended.

Thanks for all the help.
 
M

Mike Wahler

Betty Hickman said:
Sorry, I guess I should have posted the entire program, but I thought
I had included all the pertinent info. Turns out that my problem was
in my close statement---I had the wrong file name (I had copied an
earlier close statement with an input file name and failed to change
it.

Huh? 'fclose()' does not have a 'file name' parameter.

-Mike
 

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
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top