ofstream*的向量不能打开任何元素

vector of ofstream* cannot open any element

本文关键字:任何 元素 不能 向量 ofstream      更新时间:2023-10-16

在我的项目中,我试图创建一组用于输出的文件。然而,每当我试图接近这个时,我都无法打开任何流。我最好的方法是使用如下所示的ofstream指针向量,但我无法打开它们中的任何一个。

        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通常为5。Is_open()显示没有打开(废话)。我的程序编译并运行时没有输出。Perror说"无效论证"。我使用的是运行windows 10的visual studio 2013。我该怎么做才能让它发挥作用呢?谢谢。

在Windows下,文件名中不允许使用|字符。不允许使用?:<>/*"等字符。

所以我不认为你不想创建一个ofstream对象的向量。相反,你要做的是为文件名创建一个字符串向量,并使用一个ofstream对象写入每个字符串。我想象的是这样的:

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

另外,就像人们在上面评论的那样,"|"字符是保留的,不能在文件名

中使用。