Hi, I have this std::istream and I have to read exactly X bytes of data from it.
I was thinking to just read X chars because char is kind of always 1 byte but than I read that char has to be at least 8 bits so it can contain more bits.
So if it contains more bits does it mean that I would extract more bits from std::istream when reading into a char using std::istream::get(char&)?
How could I read X amount of bytes from std::istream with guarantee that it would work in every implementation?
I was thinking to just read X chars because char is kind of always 1 byte but than I read that char has to be at least 8 bits so it can contain more bits.
Yes a char is always one byte, no matter ho many bits are contained in a byte.
So if it contains more bits does it mean that I would extract more bits from std::istream when reading into a char using std::istream::get(char&)?
If you extract one char, you will always extract one byte. By the way are you talking about binary files or "normal" text files?
From the C++14 draft standard:
1
The fundamental storage unit in the C ++ memory model is the byte. A byte is at least large enough to contain any member of the basic execution character set (2.3) and the eight-bit code units of the Unicode UTF-8 encoding form and is composed of a contiguous sequence of bits, the number of which is implementation-defined. The least significant bit is called the low-order bit; the most significant bit is called the high-order bit. The memory available to a C ++ program consists of one or more sequences of contiguous bytes. Every byte has a unique address.
How could I read X amount of bytes from std::istream with guarantee that it would work in every implementation?
Assuming you're talking about binary files, there is no guarantee possible. The only "portable" file structure is a plain text file that doesn't contain anything but characters from the 8 bit portion of UTF-8 character set (basically ASCII characters).
A byte is allowed to be larger than 8 bits because they don't want to limit C++ to a specific type of hardware.
If C++ had said bytes must be exactly 8 bits (as many other languages do) it would probably mean C++ could not be used at all on hardware that used a different byte size, or it would have substantial performance implications. Question is, do you care about such hardware, and where do you find it?
Today, a byte size of 8 bits is the de-facto standard, so you might want to simply assume that. If someone made a computer that used a byte size other than 8 bits it would be incompatible with so much of the existing hardware and software that it would be hard to convince anyone to buy it. Just look at the Internet protocols (e.g. TCP/IP), the Unicode standard (UTF-8), etc. they all assume a byte is 8 bits. It's of course possible to support these protocols anyway but it would not make the best use of the hardware.
Thanks for the answers guys. I'm trying to implement HTTP client and when receiving a response from the server a "Transfer-Encoding: chunked" transfers X amount of bytes that I must read as part of that chunks data portion before going for next chunk.
So its not a binary file but just a stream of characters. So probably reading X chars will work just fine :)