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!
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!