Subject: Writing data to binary files
Posted By: maniac_ie Post Date: 2/12/2004 12:14:44 PM
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.

I was just wondering how i cud write them out as actual binary bits
and hence save on the eventual file size.

I've got something like this at the moment....

int main()
{
FILE* dude;

dude = fopen("Bin.dat", "wb");

int swap_a[4][32];

for(int e = 0; e < 4; e++){
for(int d = 0; d < 32; d=d+2){
swap_a[e][d] = 0;
swap_a[e][d+1] = 1;
}
}

for(int j=0; j<4; j++){
for(int d=0; d<32; d++){
fprintf(dude, "%i", swap_a[j][d]);
printf("%i", swap_a[j][d]);
}
fprintf(dude, "\n");
printf("\n");
}

return 1;
}

but all that does is create a file like:

01010101010101010101010101010101
01010101010101010101010101010101
01010101010101010101010101010101
01010101010101010101010101010101


How do i write each 1 or 0 as an actual bit so that they don't take up
as much space in the files as an integer?

Any help wud be well appreciated (sorry for the FILE* usage, just
moved from C)

Thanx,

Maniac.

__________________________
Play it again Sam, HARDER!

Reply By: vishal_gpt@yahoo.com Reply Date: 2/14/2004 3:28:41 AM
hi

use bit wise operators
  & and <<  


// note declare the integer d as 32bit long integer.

d = 0;
int swap_a[4][32];


for(int i=0;i,4;i++)
{
d =0;
for(int j=0;j<32;j++)
{
 d = d << 1;               // shift bits left 1 position
 d = d | swap_a[i][j];
}
fprintf(dude, "%d", d);
}

// note the resulting the file will contain only the 4 integers
Reply By: davekw7x Reply Date: 2/24/2004 11:30:29 AM
Note "minor" typo in the previous post:
  for(int i=0;i,4;i++)
should be
  for(int i=0;i<4;i++)


Now, using the formatted output function, fprintf(),
means that the file will contain the ASCII representation
of the numbers expressed in decimal (each number has a
value of 1431655765 in your example). Note that the size
of the file is 40 bytes (four 10-digit decimal numbers in
this case). In general, for different data, since the decimal
digits are all run together, it would not be possible to
read back the file to reproduce the original data (unless you
already knew what the data was).


If you want to write the four 32-bit numbers as binary bits,
try the function fwrite().

That is, instead of
  fprintf(dude, "%d", d);
try
  fwrite(&d, 4, 1, dude); // write one four-byte word as bits

Now, the output file has a size of 16 bytes (four 32-bit words).
Exactly 128 bits, which is the size of the data you are
representing.

Dave

quote:
Originally posted by vishal_gpt@yahoo.com

hi

use bit wise operators
  & and <<  


// note declare the integer d as 32bit long integer.

d = 0;
int swap_a[4][32];


for(int i=0;i,4;i++)
{
d =0;
for(int j=0;j<32;j++)
{
 d = d << 1;               // shift bits left 1 position
 d = d | swap_a[i][j];
}
fprintf(dude, "%d", d);
}

// note the resulting the file will contain only the 4 integers


Go to topic 10170

Return to index page 942
Return to index page 941
Return to index page 940
Return to index page 939
Return to index page 938
Return to index page 937
Return to index page 936
Return to index page 935
Return to index page 934
Return to index page 933