Thread: reading and writing bit to a file C++

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    1

    reading and writing bit to a file C++

    I'm writing a program using Huffman algorithm to compress text file. I have tested my program by just printing the printing ASCII character to file and it worked fine. However, now I have to implement using bits and my program doesn't work. It seems like I'm not reading or writing the right bits. Here is the result of my testing:In the input file I put abc the input file to compress it. Then I uncompress it the out out is aaa. Below is a snippet of how I read and write bits
    Code:
    class BitInput {
        istream& in;  // the istream to delegate to
        char buf;     // the buffer of bits
        int nbits; public:
    
    BitInputStream(istream& s) : in(s), buf(0), bufi(8) { }
    
    /** Read the next bit from the bit buffer.
     *  Return the bit read as the least significant bit of an int.
     */
    int readBit(){
        int i;
        if(nbits == 8){
            buf = in.get();
            nbits = 0;
        }
        i = (1 & buf>>(7-nbits)); //This could be the problem, I'm not getting the writing bit
        nbits++;
        return i;
    }
    
    /** Read a char from the ostream (which is a byte)*/
    int readChar(){
        int sum = 0;
        for(int i = 7; i>=0; i--) 
            sum = (sum*2) + readBit();
        return sum;
    }
    class BitOutput { ostream& out; // the istream to delegate to char buf; // the buffer of bits int nbits; // the bit buffer index public: BitOutput(istream& s) : in(s), buf(0), bufi(8) { } /* Write the least significant bit of the argument */ void writeBit(int i){ //Flush the buffer if(nbits == 8){ out.put(buf); out.flush(); bufi = 0; buf = 0; } buf = buf | (i<<(7-nbits)); //Did it write the right bit to ostream ? nbits++; } /** Write a char to the ostream (a byte) */ void writeChar(int ch){ for(int i = 7; i >= 0; i--) writeBit((ch >> i) & 1); }
    Last edited by joecool; 11-03-2014 at 03:00 AM.

  2. #2
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    No one will be able to help you if the question is very broad. So what is the question here? Is it how to convert a char into bit? How about some examples?

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest writing a test cases just to test these two methods (writeBit and readBit).

    Code:
    int readBit(){
        int i;
        if(nbits == 8){
            buf = in.get();
            nbits = 0;
        }
        i = (1 & buf>>(7-nbits)); //This could be the problem, I'm not getting the writing bit
        nbits++;
        return i;
    }
    Please state why you are doing nbits++ instead of nbits-- ?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You could use Boost.DynamicBitset or std::bitset if the size of the bit field is known at compile time instead of hand-rolled functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Reading and Writing in C
    By gyro25 in forum C Programming
    Replies: 2
    Last Post: 09-19-2013, 08:31 AM
  2. Reading and Writing to a file
    By m_programmer in forum C Programming
    Replies: 5
    Last Post: 10-18-2012, 12:27 PM
  3. reading/writing to file
    By JonathanS in forum C Programming
    Replies: 2
    Last Post: 01-25-2012, 12:46 AM
  4. [BIG HELP:] File Reading and Writing in C
    By kal27 in forum C Programming
    Replies: 10
    Last Post: 09-22-2011, 10:20 PM
  5. reading and writing to a file
    By rocketman50 in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2010, 12:36 PM

Tags for this Thread