作为输入,我有一个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
答案 0 :(得分:2)
迭代结束后,您的流指针无效,因为自动流对象随后被销毁。如果你真的需要,你需要在免费商店分配它们(或使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);
}
// ...
答案 1 :(得分:0)
这是破碎的;存储指向流的指针向量,但是使用指向流的本地范围自动实例的指针(在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
}
答案 2 :(得分:0)
正如评论中所提到的,我不想使用Boost而我只有gcc 4.1.2。因此,由于Cat Plus Plus的建议,使用free store的解决方案如下:
// 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];
}