My goal is to have a vector fileList of type ofstream that I could later loop through to write the same information to each of them (copying at the end is not an option). The list of files comes from argv (processed by getopt).
I understand that I need to store a pointer to the file object. Googling I was unable to find any tutorial that deals with my issue.

Docs on the issue would be nice (code would be nicer....).

you probably want vector<ofstream> fileList; Now all you have to do is loop through the names and open the files, something like this:

vector<ofstream fileList;
fileList.resize(argc-1); 
for(int i = 1; i < argc; i++)
{
    fileList[i].open(argv[i]);
}

Now to write something to all the files just use another loop similar to the above.

I guess I marked it solved to early

std::vector<std::ofstream> fileList;
            fileList.resize (argc - 1);
            for(int i = 1; i < argc; ++i)
            {
                  fileList[i].open(argv[i], ios::out);
            }

$make
/usr/include/c++/4.2/bits/ios_base.h:779: error: 'std::ios_base::ios_base(const std::ios_base&)' is private

That is why asked about pointers.

Ok, three points :

Using pointer is required since you can't copy a filestream. That is because it is tied to resource. Yes, you can use pointers. Yes it is ugly.

Note that if you want to write the same stuff to a bunch of files then it is probabily best to use a stringstream and write at the end.
You really really don't want to force the program to open too many real fstream object, you would be better off having a list of filename, a buffer for each, and then as the buffer gets filled (and on deletion -- so you can be mostly certain to write everything), you write out to all the files requiring output. (a common buffer if that is more suitable is also fine).

Yes, StuXYZ is correct about using pointers

int main (int argc, char** argv)
{
    std::vector<std::ofstream*> fileList;
    fileList.resize (argc - 1);
    for(int i = 1; i < argc; ++i)
    {
        fileList[i] = new ofstream(argv[i]);
    }
}

If you only have to write something once to each of those file then use just one ofstream object, open each file, write data, close file, continue the loop.

Note: std::out is NOT needed for ofstream because that is the default behavior.

thanks - trying now

I just noticed
Note: std::out is NOT needed for ofstream because that is the default behavior.
I know. I'm leaving it as a placeholder for when I make the CLI option to allow you to use ios::app instead.

std::vector<std::ofstream*> fileList;
            fileList.resize (argc - 1);
            for(int i = 1; i < argc; ++i)
            {
                  fileList[i] = new std::ofsteam(argv[i]);
            }

All taking about the line fileList[i] = new std::ofsteam(argv[i]);

In function 'int main(int, char**)':
error: expected type-specifier
error: cannot convert 'int*' to 'std::ofstream*' in assignment
error: expected `;'

From what I've gathered from google this happens when you forget to #include stuff but I have ofstream and vector in my includes.

std::vector<std::ofstream*> fileList;
            fileList.resize (argc - 1);
            for(int i = 1; i < argc; ++i)
            {
                  fileList[i] = new std::ofsteam(argv[i]);
            }

All taking about the line fileList[i] = new std::ofsteam(argv[i]); From what I've gathered from google this happens when you forget to #include stuff but I have ofstream and vector in my includes.

That is correct -- I didn't post a complete program. You will have to add the #include's. If you did include those and still have compile problems you need to post complete code, not just partial.

fileList = new std::ofsteam(argv);

and you can change 'ofsteam' to 'ofstream' if its like that in your original code too.

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.