1

I have written a code to read a .z compressed file directly. It works fine in linux and mac os. But it doesn't work as expected in windows.

#include <iostream>
#include <vector>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/zlib.hpp>
// cl /EHsc uncompress.cpp

std::vector<char> & readline(std::istream & stream, std::vector<char> & container) {
    char c;
    container.clear();
    while (stream && stream.get(c)) {
        container.push_back(c);
        if (c == '\n') break;
    }
    return container;
}

int main () {
    boost::iostreams::filtering_istream in;
    boost::iostreams::filtering_istream cinn(std::cin);
    in.push(boost::iostreams::zlib_decompressor());
    in.push(cinn);

    std::vector<char> line;
    while (readline(in, line).size() != 0) {
        std::string str(line.begin(), line.end());
        std::cout << "--" << str ;
    }
}

while running it as ./a.out < compressed.z on linux or mac it works fine

while running it from windows uncompressed.exe < compressed.z it doesn't show the content of the file.

Why is this behaving so ?

srbcheema1
  • 544
  • 4
  • 16

1 Answers1

0

std::cin is not opened in binary mode, so it is doing end-of-line conversions on Windows, by default corrupting all input files. There are two solutions:

  1. Never, ever use Windows, or try to write applications for it.
  2. Use _setmode( _fileno(stdin), _O_BINARY ) to convert stdin to binary mode.
Mark Adler
  • 101,978
  • 13
  • 118
  • 158