gpt4 book ai didi

c++ - boost zlib 过滤器在 Windows 中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 12:46:56 25 4
gpt4 key购买 nike

我写了一个代码来直接读取.z 压缩文件。它在 linux 和 mac os 中运行良好。但它在 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 ;
}
}

同时以 ./a.out < compressed.z 运行它在 linux 或 mac 上它工作正常

从 Windows 运行时 uncompressed.exe < compressed.z它不显示文件的内容。

为什么会这样?

最佳答案

std::cin 未以二进制模式打开,因此它在 Windows 上进行行尾转换,默认情况下会损坏所有输入文件。有两种解决方案:

  1. 永远、永远不要使用 Windows,或尝试为其编写应用程序。
  2. 使用 _setmode( _fileno(stdin), _O_BINARY )stdin 转换为二进制模式。

关于c++ - boost zlib 过滤器在 Windows 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51052520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com