473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

Similar topics

3
by: Michael Weir | last post by:
I'm sure this is a very simple thing to do, once you know how to do it, but I am having no fun at all trying to write utf-8 strings to a unicode file. Does anyone have a couple of lines of code...
13
by: E-Star | last post by:
I have a binary file that I want to read some numbers out of. However the numbers are 32bit floats. How can I get the numbers into a C program to use? I can do the calculations manually....but...
6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
2
by: Mitch | last post by:
Hi all, I hope this is the right place for this post, apologies if it is not. I am trying to write a program that creates an input file for a dispersion model. The model is expecting a Fortran...
3
by: Romain | last post by:
Hello, I am writing out a binary file. I figured that the number "10" is automaticaly converted to "OD OA" instead of "OD". "OD" and "OA" are line feed and carriage return. I know it does...
29
by: Glen | last post by:
Is it possible to write a structure to a file in c...as in c++...?? is it using fwrite?? thanx glen
1
by: Steve Bostedor | last post by:
I'm trying to use the RegistryKey to write a hex number into the registry as a binary type. I know see that in DotNet, the RegistryKey tries to interprate what registry type to create depending...
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.