Question: Start with the 1 2 - bit I / O class ofstream 1 2 and implement the matching input class ifstream 1 2 satisfying the
Start with the bit IO class ofstream and implement the matching input class ifstream satisfying the following specification:
#pragma once
#include std::byte
#include
#include
class ifstream
private:
std::ifstream fIStream;
std::byte fBuffer; input buffer
sizet fBufferSize; input buffer size
sizet fByteCount; available input bytes
sizet fByteIndex; current byte index
int fBitIndex; current bit index can be negative
void reset; reset buffer
void fetchdata; read data, std::optional readBit; read next bit
public:
using Cs nullptr
ifstream const char aFileName nullptr, sizet aBufferSize ;
~ifstream;
void open const char aFileName ;
void close;
bool isOpen const; mark bool good const; marks bool eof const;
ifstream& operator sizet& aValue ;
;
The class ifstream defines an object adapter for std::ifstream. The corresponding file input stream has to be binary and the data would constitute strings of s and s We need to use a physical bit stream to read bit values and we have to employ a buffering mechanism to first collect the bits from the underlying bit input stream and then construct bit values from the bits in the buffer.
Class ifstream requires a constructor and a destructor. The constructor has to initialize the object, acquire the necessary buffer memory, and open the input file. The destructor has to close the underlying file and free the buffer memory.
The methods open, close, isOpen, good, and eof correspond to their respective
std::ifstream methods. You should study the features of std::ifstream carefully.
The member function eof returns true, if there are no bytes left in the input stream ie fByteCount Please note that fByteCount should be if you have never read anything from fIStream. There is a subtle handshake between std::ifstream, ifstream and clients of ifstream when it comes to the detection of endoffile. The object adapter ifstream has to return true for EOF, if and only if there are no further bits available. A boundary scenario allows the underlying std::ifstream object to be in state EOF while the object adapter ifstream is not.
The function readBit implements the mapping process. It returns an optional value or The return type std::optional signifies that readBit can reach EOF while reading the next bit.
The base type of fBuffer is std::byte. Unfortunately, type std::byte offers only a limit set of operations that focus on bit manipulations and make it somewhat difficult to interpret std::byte values as plain integers. For the conversion of bit patterns to a single value, we can use the following declaration:
std::byte lByte fBufferfBufferIndex & std::byte fBitIndex;
The value of lByte is a bitmask for the bit at index fBitIndex. It is either or The value lByte is still of type std::byte. You need to apply a type conversion to sizet to interpret the value as integer using the following expression:
std::tointegerlByte
If the resulting value is greater than zero, then it denotes the bit Otherwise, it means the bit
The function readBit also triggers fetchdata, if necessary. More precisely, at the start, you must check if fByteCount is In this case, fetchdata must be called the buffer does not contain any data You may reach EOF here. In this case, readBit has no value to return.
When fetching the next bit using the expressions shown above store the result temporarily, and then advance the indices fByteIndex and fBitIndex to the next position some additional logic is required that you must devise to make it work If fBitIndex which runs from highest to lowest becomes negative, then you need to switch to the next byte in the buffer. This also means that you have processed a byte. Hence, you need to decrement fByteCount. Once all indices have been properly adjusted, you return the result.
The operator implements the readBits algorithm as shown in the tutorial. You need to adjust it to incorporate std::optional values. That is readBit may return no value when EOF has been reached In this case, you need to break from the forloop. In addition. You may need to use a static cast to sizet to set the corresponding bit in the
Writing data. Write codes Reading data.
Read codes Done
Files to be used and cant be modified:
maincpp
#include "ofstreamh
#include "ifstreamh
#include
#include
static bool write
std::cout "Write codes" std::endl;
ofstream lWriter "sample.lzw;
if lWriter.good
std::cerr "Error: Unable to open output file!" std::endl;
return false;
for sizet i ; i ;
lWriter i;
return true;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
