How to write bits from vector type bool to a file. I tried in every way, nothing helped. Writes that there should be a type of char , but I need exactly bool .

Closed due to the fact that off-topic by the participants Kromster , αλεχολυτ , user194374, Denis , VenZell Jul 22 '16 at 10:15 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Kromster, αλεχολυτ, Community Spirit, Denis, VenZell
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Exactly bits, not bytes? - Andrey Kurulev
  • one
    Show your code. - PinkTux
  • It is possible like this: std :: copy (v.begin (), v.end (), std :: ostreambuf_iterator <char> (f)); (1 file byte per 1 bit of the source array). - Vladimir Gamalyan
  • And yet specify that you have a BOOL or bool , this can be misleading (for CHAR too). - Vladimir Gamalyan
  • @VladimirGamalian, the next question: how to count an array of bits from a file with bytes :) - PinkTux

1 answer 1

A byte is the minimum independently addressable data set. A bit can exist only as part of a byte. Accordingly, it is not possible to address one bit in a language. vector<bool> is a proxy that just pretends to work with individual bits, but in fact the bits it works with are part of the bytes.

So the question as it is formulated, is simply meaningless. You can write bits in bytes - from 1 to 8 (we will not go into exotic) bits in one byte.

If you are interested in the question of how to write the entire vector at once, without dividing it into separate bits, then you need to ask a little differently, so I'm not even sure what interests you. But I will answer that as well.

In this case, the problem is that the standard, as far as I know, does not require any particular solution from the implementation, which means that there is no portable way to reach the bytes with the bits of interest. In VC ++, this is, for example, an unsigned int type, not a char or something else that is single byte. In the best case, you have to go inside the sources and get to the bottom, exactly where and how they are stored there. And it’s not a fact that this “best” case is actually not the worst: (Plus - with a very high probability - no one will allow you directly to this data due to the fact that they and their interface will be private .

  • A byte is the minimum independently addressable data set. - what prevents to address 7 bits for example? (history knows examples) - Vladimir Gamalyan am
  • Only you forgot about std :: bitset - gbg
  • @VladimirGamalian I wrote - (we will not go into the exotic) By the way, go to Wikipedia and change the definition of a byte, otherwise they do not know there that it is possible to address 7 bits with an eight-bit byte ... - Harry
  • one
    @Harry, the author is trying to shave with a bowl. I suggest he use a razor - suddenly, it will be more convenient. And I notice to you that in C ++ there is a standard tool for working with individual bits, and not the only one. - gbg
  • one
    @gbg Well, you suggested. Within the framework of the question asked, I explained why the bowl is uncomfortable :) - Harry