472,344 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,344 software developers and data experts.

write 2D array to binary file

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
6 9863
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

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: chris | last post by:
I'm writing a small app to help me learn more about cryptography. All it does is encrypt all of the files in directory A, and put the encrypted...
5
by: rob | last post by:
hey every1, I've got alot of data to write out to file and it's all just 1's and 0's. It's all stored in 2 dimensional arrays of width 32 and...
20
by: cylin | last post by:
Dear all, I open a binary file and want to write 0x00040700 to this file. how can I set write buffer?...
0
by: Bahaa Hany | last post by:
that the code i wrote FileStream w = new FileStream("d:\\HufmanCompression.txt",FileMode.Create); BinaryWriter writer = new...
3
by: post | last post by:
<?php function strRandom($length, $amount, $flag){ for ($i=0; $i<$amount; $i++){ $my_Array = randomkeys_($length, $flag); echo...
13
by: zach | last post by:
Can someone help me out, I can't figure out what I'm doing wrong to write to a file in binary mode. What's wrong with my code? <?php ...
2
by: Vikashag | last post by:
Hi When I am trying to write in a binary file its giving me error. Now What I have to do? and in this Base64Decode2 is converted Binary data....
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.