Results 1 to 8 of 8

Thread: writing/adding 1 bit into stream

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2004
    Posts
    6

    writing/adding 1 bit into stream

    I have a file which I'm trying to write to. I opened it with an object of fstream class in binary mode and out mode (ios::binary | ios:ut).

    Now, I have no problems reading/writing characters to the file. I also have no problems reading/writing a structure to the file.

    Code:
    struct record
    {
       int i;
       char *name;
    }student;
    
    void write()
    {
       fstream newFile;
    
       //yada yada bla bla... (Insert rest of codes here)   
    
       newFile.write(reinterpret_cast<const char *>(&student);
       newFile.write("Male");
    }
    This code is output in binary format, thus, when you open the text file with notepad, some of it is intelligible.

    The problem is, now I want to write a binary file, inputting the binary streams in variable sized length of bits.

    Eg. I want to output "1010 100 001111 011 01 1100" to the file, as binary code, not characters.

    is there any way I can do this?

    1) I've tried using bool datatype with true/false. but when I write the boolean variable into the file, it still outputs them in 8 bit chunks.
    I checked in the debugger and boolean datatypes take up 1 whole byte although there are only 2 combinations available. "0x00" AND "0x01"

    2) i tried enumerating my own boolean values, but there seems to be no binary format in C++. "0b". is there any other way that i don't know about, or did i do anything wrong?

    3) is there anyway to do this with bitwise manipulation? 'cos all the bitwise manipulations i know doesn't add bits to a stream, but it just change their values.

    4) is there an already known and available method for writing just 1 bit to a stream?


    any help would be appreciated, thank you.
    Calvin (of Calvin and Hobbes) - It's a funny world Hobbes. But it's not a hilarious world.

    I am welcome to you, if you come to Here

  2. #2
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    1. Writing a bool will write at least one whole byte.
    2. ?
    3. Yes it is possible to write single bits using bitwise operators, you first store your bits untill they form a whole byte, then you write that byte to the file.
    4. no

    But I wonder, why do you want to do this? The only uses I can think of are related to huge arrays of bits or data compression.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2004
    Posts
    6
    wow. u know ur stuff!

    data compression it is

    thx for the clarifications, i'll c what i can do according to ur guidelines. but expect more newbie questions in the future
    Calvin (of Calvin and Hobbes) - It's a funny world Hobbes. But it's not a hilarious world.

    I am welcome to you, if you come to Here

  4. #4
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    When you are outputing to the stream, you want to package the bits into bytes and then output each byte to the stream.

    I think I have some code lying around for packing bits if you want it.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2004
    Posts
    6
    a little help from sample codes wouldn't hurt .


    how do i get u?
    Calvin (of Calvin and Hobbes) - It's a funny world Hobbes. But it's not a hilarious world.

    I am welcome to you, if you come to Here

  6. #6
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    Its my teacher's code that he made us write first.

    We had to use this in a compression project so it should work.
    Attached Files Attached Files
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  7. #7
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    The header for this.
    Attached Files Attached Files
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jun 2004
    Posts
    6
    thanks a bunch =)
    Calvin (of Calvin and Hobbes) - It's a funny world Hobbes. But it's not a hilarious world.

    I am welcome to you, if you come to Here

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width