darxsys
darxsys

Reputation: 1570

Read a file in parts, continue after the last read part

I've been googling this for some time now to no avail and I am not even sure if there is a name for this.

My use case is: I have a big file that contains blocks of text that should be independently processed. They are all listed in the file one after the other and separated with a delimiter. I would like to read that file block by block and do some operations on the read block and then read the next block. Something like iterating through a file, but not reading all at once to memory. I have java code that does that through some kind of BlockGen class that implements iterable interface and reads the file from previously read position when client code needs it, but I am very not sure how to implement this in C++ properly.

Any help appreciated. And yeah, it's not homework!

Upvotes: 0

Views: 280

Answers (1)

doctorlove
doctorlove

Reputation: 19282

For fixed block sizes, you can use ifstream::read

In your case, it won't be too hard to roll your own BlockGen:

Given a definition of Block e.g. a std::vector<std::string>, and a class (with suitable though given to copy constructors etc)

class BlockGen
{
    public:
    //boilerplate constructors, taking delimiter between blocks
    // opening input etc, etc

    Block getNextBlock()
    {
        Block block;
        std::string line;
        while( std::getline(input, line)  && line!=delimiter)
        {
            block.push_back(line);
        }
        return block;
    }

    private:
    std::ifstream input;
    std::string delimiter;
};

Upvotes: 1

Related Questions