469,648 Members | 1,557 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,648 developers. It's quick & easy.

Writing Numbers to a Binary File

rob
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,

Rob.
Jul 22 '05 #1
5 5328
rob wrote:
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,

Rob.


You are using text mode for writing to the file. The
fprintf function converts from binary (native) format
to text format.

Use the fwrite function:
fwrite(swap_a, sizeof(swap_a), 1, dude);
Be sure to check the return value.

Also, when writing out as binary, don't append '\n'
to the output.

When opening a file in binary mode, you are just
telling the I/O system not to translate any characters.
Some platforms translate '\n' into '\r\n' and vice
versa.

Also, when you get a chance, look at the fstream
and iostream classes. They are the C++ streams.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #2
rob wrote:

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.


By eg. collecting 8 of them and forming an unsigned char of them

eg.

unsigned char Bits = Bit0 << 7 |
Bit1 << 6 |
Bit2 << 5 |
Bit3 << 4 |
Bit4 << 3 |
Bit5 << 2 |
Bit6 << 1 |
Bit7;

Also note, that you might want to use fwrite instead of fprintf to
write the bits.

fwrite( &Bits, 1, 1, dude ); // look Ma, 8 of my bits only account for
// 1 Byte on the file
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Rob,

Well, first, I'm going to use the C++ container called bitset provided
by Boost. Then, I'm going to use the iostreams library to write out the
data in binary format.

I chose the boost::dynamic_bitset library because it has the option of
creating a runtime sized "container" of bits which can then be striped
from the "container" in block format, where the blocks are some integral
time like unsigned int. Finally, by using the iostream write function,
the data is sent to the output file in binary format.

rob wrote:
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;
}
}

[snip]


//C++ news group bitset operations

#include <fstream>
#include <boost/dynamic_bitset.hpp>

using namespace std;
using namespace boost;

int main(){
typedef unsigned int block;
typedef dynamic_bitset<block> bitset;
typedef block BlockItor;

int numblocks = 4;
bitset swap_a(numblocks); //make a bitset 4 ints long...All bits
default to 0
ofstream dude("Bin.dat");
BlockItor outitor;

//Now, set every other bit to 1 (actually, this doesn't work,
//but you get the idea.
for (int i = 0; i < (swap_a.num_blocks() * sizeof(block)); i++)
swap_a[i].flip();

//Finally, grab the blocks off the set & write them out to the stream
to_block_range(swap_a, &outitor);
for(int i = 0; i < swap_a.num_blocks(); i++){
dude.write((const void *)&outitor, sizeof(block));
outitor++;
}
dude.close();
return 0;
}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFALO8joo/Prlj9GScRArcSAJ0cq4gLHcKsptbV8sGivxPryOyxaACeKqA6
qnUO7NxC9hKa/waeI0l2Gdw=
=m9j2
-----END PGP SIGNATURE-----
Jul 22 '05 #4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Darn it! I flubbed a critical part of the output where the iterator is
advancing. Correction to previous code below.

Evan Carew wrote:

//Finally, grab the blocks off the set & write them out to the stream
to_block_range(swap_a, &outitor);
for(int i = 0; i < swap_a.num_blocks(); i++){
dude.write((const void *)&outitor, sizeof(block));
outitor++;
}

Should read:

//Finally, grab the blocks off the set & write them out to the stream
to_block_range(swap_a, &outitor);
for(int i = 0; i < swap_a.num_blocks(); i++){
dude.write((const void *)&outitor, sizeof(block));
to_block_range(swap_a, &outitor);
}
Sorry about that.

Evan Carew
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFALO/0oo/Prlj9GScRAg80AJ9cPcAjsqN/2QPfbIZx4t92hCtPTQCeK2Yv
QOBcZ4zG/WDaqIUBSl7pdlo=
=mq/l
-----END PGP SIGNATURE-----
Jul 22 '05 #5
I had done something similar in the past. Here's a function I wrote:

/************************************************** ********
FUNCTION: packint
DESCRIPTION: Takes in an array of 32bit integers, and puts them into a
serialized binary string structure.
INPUT: char array pointer, int array pointer, int array size
OUTPUT: (modifies char array pointer)
SYNTAX:
packint( binary result string, array of int, # of array elements);
************************************************** *******/
void packint (u_char *buffer, const unsigned long int *intarray, const
unsigned long int size)
{
unsigned long int c, i;

c = 0;
for(i = 0 ; i < size; i++)
{
buffer[c]=(u_char)(intarray[i] >> 24);
buffer[c+1]=(u_char)(intarray[i] >> 16);
buffer[c+2]=(u_char)(intarray[i] >> 8);
buffer[c+3]=(u_char)(intarray[i]);
c=c+4;
}
}
All you have to do now is just write the result string to a BINARY file
or a database. You could even process the string thru a compression
function to save even more space.

Of course I was storing 32-bit wide numbers, but it could easily be
modified for any bit size (24, 16 or 8-bit) if you know your max ranges.

-John_JR



Thomas Matthews wrote:
rob wrote:
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,

Rob.

You are using text mode for writing to the file. The
fprintf function converts from binary (native) format
to text format.

Use the fwrite function:
fwrite(swap_a, sizeof(swap_a), 1, dude);
Be sure to check the return value.

Also, when writing out as binary, don't append '\n'
to the output.

When opening a file in binary mode, you are just
telling the I/O system not to translate any characters.
Some platforms translate '\n' into '\r\n' and vice
versa.

Also, when you get a chance, look at the fstream
and iostream classes. They are the C++ streams.


Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Michael Weir | last post: by
13 posts views Thread by E-Star | last post: by
6 posts views Thread by Sebastian Kemi | last post: by
3 posts views Thread by Romain | last post: by
29 posts views Thread by Glen | last post: by
1 post views Thread by Steve Bostedor | last post: by
3 posts views Thread by nicolasg | last post: by
6 posts views Thread by arne.muller | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.