I have a class that has functions which use <stdio.h> and <stdarg.h> to take in indefinite arguments, and one of the member functions taking in filenames. There's another function (call it the "do" function) where I want to read from ifstreams created from those indefinite number of filenames.

I can create a vector<string> and then create new ifstreams in the do() function each time it's called, and use seekg(). But in this case the do() function is called millions of times, the files are dozens of megabytes in size, and the file reads I want to do are sequential and this method would seek from ios::beg each time. So that would be incredibly inefficient.

I thought one solution would be to create a vector<ifstream> as a member variable and read using the seekg() ios::cur option for each of the ifstream vector elements. However I found that's not possible because you can't create a vector of ifstream elements.

Does anyone have any ideas on how I might better approach this?

How about using threads? One thread for each file. That way you wouldn't have to open and close each file each time you need to read it, so you wouldn't have to start reading from the beginning.

Did you try something like this?

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

void foo(ifstream& in)
{

}

int main()
{
    vector<ifstream*> streams;
    for(size_t i = 0; i < 5; i++)
    {
        ifstream* in = new ifstream;
        streams.push_back(in);
        streams[i]->open("filename.txt");
    }
    foo( *streams[1] );
}

Dragon's approach will work .... the only other obstacle is the fact that most systems allow a process to have only a small number of files open simultaneously. If you exceed that number, .....

Some remarks about terminology.
The C++ Standard use clauses "variable parameter list" and "function that can be called with varying number and types of arguments" for functions with ellipsis in parameter list.
As usually authors on this forum use a rather strange term "function with indefinite arguments".
Is it OK?

How about using threads? One thread for each file. That way you wouldn't have to open and close each file each time you need to read it, so you wouldn't have to start reading from the beginning.

Sounds like a good idea, but unfortunately I'm not familiar with threading.

Did you try something like this?

Thanks a lot Ancient Dragon, that solution works :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.