I try to read the file with specific extension .compressed. I created it using Fano encoding:
std::ofstream out;
out.open(file_name + ".compressed", std::ios::out | std::ios::binary);
in.open(file_name, std::ios::in | std::ios::binary);
in >> std::noskipws;
if (!out) {
out.close();
in.close();
return false;
}
wchar_t sym;
char buffer = 0;
int bit = 1;
while(in >> sym) {
for (wchar_t b : fano_codes[sym]) {
if (b == '1') buffer |= 0b00000001;
if (bit == CHAR_BIT) {
out << buffer;
buffer = 0;
bit = 1;
}
else {
buffer <<= 1;
bit++;
}
}
}
if (bit != 0) {
for (; bit <= CHAR_BIT; bit++) {
buffer <<= 1;
}
out << buffer;
}
out.close();
in.close();
So when I try to read this file it doesn't work.
std::wifstream in;
in.imbue( std::locale(std::locale(), new std::codecvt_utf8<wchar_t>) );
in >> std::noskipws;
in.open(file_name, std::ios::binary | std::ios::in);
if (!in) {
in.close();
return false;
}
wchar_t sym;
std::wstring file_code;
while (in >> sym) { // this 'while' always is skipped
file_code += sym;
}
in.close();
I used many modes (such as std::ios::binary) in open() function, checked all flags in stream and tried to use get() instead of >>, nothing worked and I don't know why.