Unix Programming - Bitwise file writing

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2006 > Bitwise file writing





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Bitwise file writing
shibdas

2006-05-29, 5: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

moi

2006-05-29, 5: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
Barry Margolin

2006-05-29, 5: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 ***
noogie.brown@gmail.com

2006-05-29, 5: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;
}

Roy L Butler

2006-05-30, 1: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
shibdas

2006-05-30, 7:16 pm

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

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com