Jeff
Jeff

Reputation: 724

vector of ofstream* cannot open any element

In my project, I am trying to create a set of files for output. However, I am unable to open any of the ofstreams whenever I try to approach this. My best approach would use a vector of ofstream pointers shown below, but I am unable to open any of them.

        vector<ofstream*> out;
        for (int m = 0; m < p; m++)
        {
            for (int n = 0; n < p; n++)
            {
                string outname = "TLR" + to_string(n) + "|" + to_string(m) + ".txt";
                out.push_back(new ofstream{ outname.c_str() });
            }
        }

p is typically 5. is_open() reveals that nothing opens (duh). My program compiles and runs with no output. perror says "invalid argument." I am on visual studio 2013 running windows 10. What can I do to make this work? Thanks.

Upvotes: 3

Views: 121

Answers (2)

BCza
BCza

Reputation: 340

So I don't think you don't want to create a vector of ofstream objects. What you instead want to do is create a vector of strings for file names, and write to each one using one ofstream object. What I'm imagining is something like this:

vector<string> out;
for (int m = 0; m < p; m++)
    for (int n = 0; n < p; n++)
        out.push_back("TLR" + to_string(n) + "_" + to_string(m)+ ".txt");

// Then iterate over the files writing to each one whatever you'd like with the ofstream

Also, like people have been commenting above, the "|" character is reserved and cannot be used in a file name

Upvotes: 0

Miles Budnek
Miles Budnek

Reputation: 30619

The | character is not allowed in filenames on Windows. The ?,:,<,>, \, /, *, and " characters are also not allowed. All other printable unicode characters are valid.

Upvotes: 5

Related Questions