By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,191 Members | 1,256 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,191 IT Pros & Developers. It's quick & easy.

write 2D array to binary file

P: n/a
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[i] = 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[i],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[i],ncolumns*sizeof(short),1,outfile);

and

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

but neither works correctly.

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

Thanks!
Nov 14 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Betty Hickman wrote:
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[i] = 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[i] = calloc(ncolumns, sizeof *clump_class[i]);

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

--
Er*********@sun.com

Nov 14 '05 #2

P: n/a

"Betty Hickman" <hi**********@hotmail.com> wrote in message
news:4b**************************@posting.google.c om...
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[i] = 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[i],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[i],ncolumns*sizeof(short),1,outfile);

and

for (i=0; i<nrows; ++i)
for (j=0; j<ncolumns; ++j)
fwrite(&clump_class[i][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
Nov 14 '05 #3

P: n/a
Betty Hickman wrote:
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[i] = 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[i],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[i],ncolumns*sizeof(short),1,outfile);

and

for (i=0; i<nrows; ++i)
for (j=0; j<ncolumns; ++j)
fwrite(&clump_class[i][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.

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #4

P: n/a
On Wed, 10 Nov 2004 23:16:52 UTC, hi**********@hotmail.com (Betty
Hickman) wrote:
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[i] = 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[i],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[i],ncolumns*sizeof(short),1,outfile);

and

for (i=0; i<nrows; ++i)
for (j=0; j<ncolumns; ++j)
fwrite(&clump_class[i][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.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation

Nov 14 '05 #5

P: n/a
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.

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.

Nov 14 '05 #6

P: n/a

"Betty Hickman" <hi**********@hotmail.com> wrote in message
news:4b**************************@posting.google.c om...
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
Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.