eggitta
eggitta

Reputation: 1

How to seekg on a gz file

I'm working on a gzip file reader to handle huge .gz files. I want to split a file into multiple chunks, and record the start position of each chunk, then the chunked data can be processed parallelly.

However, getline always fails after seekg.

Is it possible to seekg on a gzip file?

My code is like:

int main() {
    // Open the gzipped file
    std::ifstream file("my.gz", std::ios::binary);

    // Create a filtering stream for decompression
    boost::iostreams::filtering_stream<boost::iostreams::input> decompressedStream;
    decompressedStream.push(boost::iostreams::gzip_decompressor());
    decompressedStream.push(file);

    // Read the first line
    std::string line;
    std::cout << "firstLineStartPos: " << file.tellg() << std::endl;
    std::getline(decompressedStream, line);
    std::cout << "First line: " << line << std::endl;

    // Record the start position of the second line
    std::streampos secondLineStartPos = file.tellg();
    std::cout << "secondLineStartPos: " << secondLineStartPos << std::endl;

    // Read the second line
    std::getline(decompressedStream, line);
    std::cout << "Second line: " << line << std::endl;

    // Jump back to the start position of the second line
    file.clear();  // Clear any error flags in the file stream
    file.seekg(secondLineStartPos);

    // Recreate the decompression stream
    decompressedStream.reset();  // Clear the existing data from the filtering stream
    decompressedStream.push(boost::iostreams::gzip_decompressor());
    decompressedStream.push(file);

    // Read the second line again
    std::getline(decompressedStream, line);
    std::cout << "Second line (again): " << line << std::endl;

    return 0;
}

And, the output is like:

firstLineStartPos: 0
First line: # item1 item2 item3 item4 item5 item6
secondLineStartPos: 8192
Second line: Bump_0 uBump_1 0.00000 0.00000 VDD1 VDD1
Second line (again): 

The getline fails after seekg?

Upvotes: 0

Views: 73

Answers (0)

Related Questions