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! 6 10028
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
"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
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 ---
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
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.
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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 versions of the files in directory B. It then...
|
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 varying
height.
At the moment it's all just...
|
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?
---------------------------------------------------
typedef unsigned char UCHAR;
int...
|
by: Bahaa Hany |
last post by:
that the code i wrote
FileStream w = new FileStream("d:\\HufmanCompression.txt",FileMode.Create);
BinaryWriter writer = new BinaryWriter(w);
for (i = 0; i <...
|
by: post |
last post by:
<?php
function strRandom($length, $amount, $flag){
for ($i=0; $i<$amount; $i++){
$my_Array = randomkeys_($length, $flag);
echo "{$my_Array}";
$nbsp = "\r\n";...
|
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
$fileName = "something.dat";
$string = "This is a...
|
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.
Its giving the errror
Arguments are of the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |