Bitwise file writing
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Bitwise file writing




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Bitwise file writing  
shibdas


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-29-06 10:32 PM

Hi,
For a compression program I have to write data to a file in 13
bit chucks. fread and fwrite reads bytewise. Is there any low level
function using with I can read/write into a file in other than 8
bits(like 13 bits) chuck??

Thanks






[ Post a follow-up to this message ]



    Re: Bitwise file writing  
moi


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-29-06 10:32 PM

shibdas wrote:
> Hi,
>        For a compression program I have to write data to a file in 13
> bit chucks. fread and fwrite reads bytewise. Is there any low level
> function using with I can read/write into a file in other than 8
> bits(like 13 bits) chuck??

No.

AvK





[ Post a follow-up to this message ]



    Re: Bitwise file writing  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-29-06 10:32 PM

In article <0JqdnZgSzoC9ROTZnZ2dnUVZ8sidnZ2d@casema.nl>,
moi <avk@localhost> wrote:

> shibdas wrote: 
>
> No.

The way to do it is to use bitwise operators to write to a memory
buffer, and then write the buffer to the file.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***





[ Post a follow-up to this message ]



    Re: Bitwise file writing  
noogie.brown@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-29-06 10:32 PM

For a file compression program I was writing I used the following,
which worked ok to some extent. Back then I was into c++ though, but
you should be able to see the principle. Basically its just a one-byte
buffer...
Read returns a bit at a time, and when its out of bits gets a new byte.
Write lets you write bits to it using the function. and when it's byte
is full, it dumps it out.

/*
bitwise input file class
*/

bif::bif(ifstream* f)
{
file = f;
bitnum = 7;
file->get(b);
bytes = 1;
}

bool bif::get()
{
if(bitnum<0)
{
if(file->eof())
{
return 0;
}
else
{
bitnum = 7;
file->get(b);
bytes++;
}
}
byte r = b & (byte)(1<<bitnum);
bitnum--;
return (bool)r;
}

bool bif::eof()
{
return (bitnum<0);
}

dword bif::bytesread()
{
return bytes;
}



/*
bitwise output file class
*/

#include <fstream.h>
#include <math.h>

#include "bof.h"
#include "defs.h"

bof::bof(ofstream* f)
{
file = f;
bitnum = 7;
b = 0;
bytes = 0;
}

void bof::put(bool bit)
{
b += (bit << bitnum);
bitnum--;
if(bitnum<0)
{
bitnum = 7;
file->put(b);
bytes++;
b = 0;
}
}

byte bof::expunge()
{
if(bitnum==7) return 0;  //nothing to do
bitnum = 7;
file->put(b);
b = 0;
return (8 - bitnum);
}

dword bof::byteswritten()
{
return bytes;
}






[ Post a follow-up to this message ]



    Re: Bitwise file writing  
Roy L Butler


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-30-06 06:15 PM

Barry Margolin wrote:
> In article <0JqdnZgSzoC9ROTZnZ2dnUVZ8sidnZ2d@casema.nl>,
>  moi <avk@localhost> wrote:
> 
>
> The way to do it is to use bitwise operators to write to a memory
> buffer, and then write the buffer to the file.
>

... and perform the disk I/O from/to the memory buffer in much greater
then 2 byte chunks if you care at all about performance, especially if
over a networked file system.


Roy





[ Post a follow-up to this message ]



    Re: Bitwise file writing  
shibdas


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-31-06 12:16 AM

Thanks. I am using memory buffer to hold the data temporarily before
writing it out to the disk bytewise..






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 11:09 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register