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_cl ass[i],sizeof(short), ncolumns,outfil e);
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_cl ass[i],ncolumns*sizeo f(short),1,outf ile);
and
for (i=0; i<nrows; ++i)
for (j=0; j<ncolumns; ++j)
fwrite(&clump_c lass[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 10078
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**********@h otmail.com> wrote in message
news:4b******** *************** ***@posting.goo gle.com... 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_cl ass[i],sizeof(short), ncolumns,outfil e);
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_cl ass[i],ncolumns*sizeo f(short),1,outf ile);
and
for (i=0; i<nrows; ++i) for (j=0; j<ncolumns; ++j) fwrite(&clump_c lass[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_cl ass[i],sizeof(short), ncolumns,outfil e);
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_cl ass[i],ncolumns*sizeo f(short),1,outf ile);
and
for (i=0; i<nrows; ++i) for (j=0; j<ncolumns; ++j) fwrite(&clump_c lass[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.bi n", "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**********@ho tmail.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_cl ass[i],sizeof(short), ncolumns,outfil e);
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_cl ass[i],ncolumns*sizeo f(short),1,outf ile);
and
for (i=0; i<nrows; ++i) for (j=0; j<ncolumns; ++j) fwrite(&clump_c lass[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**********@h otmail.com> wrote in message
news:4b******** *************** ***@posting.goo gle.com... 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 decrypts the
files in directory B, and puts them in directory C. I'm getting some
exceptions when I try to decrypt a binary file though. Here's the
code:
//////////////////////////////////////////////////////////////////////
public class...
|
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 integer arrays and the individual 1's and
0's are being written out as integers.
|
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 iFD=open(szFileName,O_CREAT|O_BINARY|O_TRUNC|O_WRONLY,S_IREAD|S_IWRITE);
UCHAR buffer; //???????????
write(iFD,buffer,5);
---------------------------------------------------
|
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 < array.Length;i++ )
writer.Write(array);
w.Close();
The output i need is to write a binary file with a very small size(it is a compression prog)
thanx for ur time
|
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";
file_put_contents("h.html", $my_Array);
file_put_contents("h.html", $nbsp);
echo "<br />";
}
| |
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 string of text";
|
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 wrong type, are out of acceptable range, or are in conflict with one another.
at oStream.write(Base64Decode2) position
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |