【问题标题】:Read gzipped files by using a STL vector of pointers to igzstreams使用指向 igzstreams 的指针的 STL 向量读取 gzip 压缩文件
【发布时间】:2026-01-20 23:05:02
【问题描述】:

作为输入,我有一个 gzip 文件列表。如图here,我使用gzstream来处理它们。出于实际原因,我想打开每个文件并将每个流记录到一个向量中。这看起来很简单,但我无法让它发挥作用。这是最小的代码:

#include <cstdlib>

#include <iostream>
#include <vector>
using namespace std;

#include <gzstream.h>

int main (int argc, char ** argv)
{
  size_t i;
  vector<string> vInFiles;
  vector<igzstream *> vStreams;
  string line;

  // create the dummy input files
  system ("rm -f infile*.gz; for i in {1..2}; do echo \"toto\"${i} | gzip > infile${i}.gz; done");
  vInFiles.push_back ("infile1.gz");
  vInFiles.push_back ("infile2.gz");

  // open each input file
  for (i = 0; i < vInFiles.size(); ++i)
  {
    igzstream inStream;
    inStream.open (vInFiles[i].c_str());
    if (! inStream.good())
    {
      cerr << "ERROR: can't open file " << vInFiles[i] << endl;
      exit (1);
    }
    vStreams.push_back (&inStream);
  }

  // manipulate each input file
  for (i = 0; i < vInFiles.size(); ++i)
  {
    cout << "read first line of file " << vInFiles[i] << endl;
    getline (*(vStreams[i]), line);
    if (line.empty())
    {
      cerr << "empty line" << endl;
      exit (1);
    }
    cout << line << endl;
  }

  // close each input file
  for (i = 0; i < vInFiles.size(); ++i)
  {
    vStreams[i]->close();
  }
  vStreams.clear();

  return 0;
}

这段代码编译正确:

$ gcc -Wall test.cpp -lstdc++ -lgzstream -lz

虽然它运行顺利,但它不能正确读取文件:

$ ./a.out
read first line of file infile1.gz
empty line                

【问题讨论】:

  • 内嵌 vStreams.push_back (&​​inStream);您将指向流的指针推送到第一个 for 循环的范围内,该流是自动和本地对象。流对象在每次迭代的每次退出时都会被销毁,留下指向不存在对象的悬空指针。

标签: c++ gzip


【解决方案1】:

迭代结束后,您的流指针无效,因为自动流对象随后被销毁。如果您确实需要,您需要在免费商店中分配它们(或使igzstream 可移动)。

// std::vector<boost::shared_ptr<igzstream>> for C++03 
std::vector<std::unique_ptr<igzstream>> vStreams;

// ...

for (size_t i = 0; i < vInFiles.size(); ++i) {
    // boost::shared_ptr<igzstream> inStream = boost::make_shared<igzstream>();
    auto inStream = std::unique_ptr<igzstream>(new igzstream);
    inStream->open(...);
    // ...
    vStreams.push_back(inStream);
}

// ...

【讨论】:

  • 感谢您的回答。因为我只有 gcc 4.1.2(不能使用 -std=c++0x 编译),所以我不会使用 unique_ptr。相反,我将使用newdelete 作为我的流对象,以便在空闲存储中分配它们。我会添加我自己的答案。
  • @woolhill:那么你应该使用shared_ptr
  • 在 gcc 4.1.2 中,您可以使用 boost::scoped_ptr,它与 c++11 unique_ptr 基本相同。您可以在 boost.org 找到 boost 库。
【解决方案2】:

这个坏了;您存储一个指向流的指针向量,但您使用指向本地范围内的流的自动实例的指针(在 for 循环内)对其进行初始化。一旦循环的每次迭代完成,该实例就会超出范围,并且您有一个指向一些废话的指针。

然后你再用那个废话,然后你就废话了。

使用智能指针,例如

  std::vector<boost::shared_ptr<igzstream> > vStreams;
  // to initialize
  for (i = 0; i < vInFiles.size(); ++i)
  {
    boost::shared_ptr<igzstream> inStream(new igzstream(vInFiles[i].c_str());
    if (!inStream->good())
    {
      cerr << "ERROR: can't open file " << vInFiles[i] << endl;
      exit (1);
    }
    vStreams.push_back (inStream); // save the smart pointer
  }

【讨论】:

  • 感谢您的提示,我不知道。但我暂时不想使用 Boost。
【解决方案3】:

正如 cmets 中提到的,我不想使用 Boost,而且我只有 gcc 4.1.2。因此,这里是使用free store 的解决方案,感谢 Cat Plus Plus 的建议:

  // open each input file
  for (i = 0; i < vInFiles.size(); ++i)
  {
    igzstream * pt_inStream = new igzstream;
    pt_inStream->open (vInFiles[i].c_str());
    if (! pt_inStream->good())
    {
      cerr << "ERROR: can't open file " << vInFiles[i] << endl;
      exit (1);
    }
    vStreams.push_back (pt_inStream);
  }

还有:

  // close each input file                                                                                                                                                                                                                                           
  for (i = 0; i < vInFiles.size(); ++i)
  {
    vStreams[i]->close();
    delete vStreams[i];
  }

【讨论】: