How to read bit by bit in a file

This is a discussion on How to read bit by bit in a file within the C++ Programming forums, part of the General Programming Boards category; My question is that assume that you have a file with hundred's of 0's or 1's (binary) example: 10010101010101101 10101010101010111 ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    37

    How to read bit by bit in a file

    My question is that


    assume that you have a file with hundred's of 0's or 1's (binary)
    example:
    10010101010101101
    10101010101010111
    .......


    Now I want to read one bit at a time from that file. Is that possible?
    If there was a space " " in between each bits then Yes I could do it by
    Code:
        
    long long int dataDECODER[512];
    int j = 0;
    ifstream inBINARY;    
    inBINARY.open("outBinaryFile.txt");
    while(!inBINARY.eof())
        {
           inBINARY >> dataFILE[j];
               j++;        
        }



    I cannot grab the whole data into an array (if there were to be no spaces in between) or a variable because the value in the file is too large.

    What I am trying to do is reading each bit and use "Tree Branch" method for the Huffman.


    So is there a way to do this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    It sounds like rather than read bit by bit, you want to read character by character, and interpret each character as a bit.

    Incidentally, boost::dynamic_bitset might come in handy here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    37
    I am looking at the examples of boost::dynamic_bitset. So far I am seeing that it sets or clear the bits.
    How is that reading the data in the file.

    I think you are right about reading char by char instead of bit by bit, but
    how would you read each char individually?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    Quote Originally Posted by geewhan
    I am looking at the examples of boost::dynamic_bitset. So far I am seeing that it sets or clear the bits.
    How is that reading the data in the file.
    Refer to the overloaded operator>> for input streams.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 06-18-2012, 08:23 AM
  2. Replies: 1
    Last Post: 02-21-2012, 03:54 PM
  3. Open a file, read it ... and ... read it again
    By Tiago in forum C Programming
    Replies: 1
    Last Post: 04-17-2010, 03:32 AM
  4. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21