Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 12th, 2004, 01:14 PM
Registered User
 
Join Date: Feb 2004
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Writing data to binary files

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 With Quote
  #2 (permalink)  
Old February 14th, 2004, 04:28 AM
Registered User
 
Join Date: Nov 2003
Location: varanasi, up, India.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 With Quote
  #3 (permalink)  
Old February 24th, 2004, 12:30 PM
Authorized User
 
Join Date: Feb 2004
Location: , , .
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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:
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Manipulate numeric data in binary files pkipe Pro VB 6 7 March 12th, 2008 03:35 PM
Binary files MAXhYpe C# 0 November 2nd, 2004 04:42 AM
Reading from Binary Files jschmi J2EE 0 October 5th, 2004 01:09 AM
Reading data from files and writing it to database enggalok VS.NET 2002/2003 1 February 24th, 2004 12:33 AM
Reading from a text file and writing to a Binary 1 scott01 C++ Programming 0 July 28th, 2003 03:59 AM



All times are GMT -4. The time now is 09:39 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.