Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 12th, 2004, 01:14 PM
Registered User
 
Join Date: Feb 2004
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
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
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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.