如何使用数组中的字符串打开文件?

我有一个需要打开的文件名数组。当我放入plans.open。它给我一个错误“没有匹配函数来调用'std :: basic_ifstream :: open(std :: __ cxx11:...'')

#include <iostream>
#include <fstream>
#include <cstring>
#include <string>

using namespace std;

int main(){
    ifstream files;
    ifstream plans;
    string stufiles[100];
    int numFiles,timeBlocks;
    files.open("filesToProcess.txt");
    if (files.fail()){ //checks to see if the selected store file opened
        cout << "Error when opening file!" << endl;
        return 0;
    }
    files >> numFiles;
    for (int i= 0; i<= numFiles; i++) {
         files >> stufiles[i];
    }
    files.close();
    cout << stufiles[0] << endl;
    plans.open(stufiles[0]);
    if (plans.fail()){ //checks to see if the selected store file opened
        cout << "Error when opening file!" << endl;
        return 0;
    }
}

这应该使用数组中的文件名打开文件。 它给我一个错误“没有匹配函数来调用'std :: basic_ifstream :: open(std :: __ cxx11:...'')

k702059455 回答:如何使用数组中的字符串打开文件?

您的编译器版本std::ifstream::open()不支持std::string作为输入,因此您必须给它一个const char*。您可以为此使用std::string::c_str()

plans.open(stufiles[0].c_str());
本文链接:https://www.f2er.com/3155508.html

大家都在问