将多个.text文件读入vector <double> </double>

时间:2012-11-12 20:05:13

标签: c++

美好的一天,

我编写了以下函数,它是一些计算的一部分:

vector<double> read(){
    cout << "Add file to calculate, example: name.txt" << endl;
    char file;
    double numz;
    vector<double> myvector;
    char str[256];
    ifstream fin;
    cin.get (str,256);
    fin.open (str);
    while(fin.good()){

            fin>>numz;
            myvector.push_back(numz);

    }
    return myvector;


}  

此函数读取带有数字的单个.txt文件,并将它们保存到为其他函数中的进一步计算返回的向量。

该功能正常,但我想编辑它以使用保存的多个.txt文件,例如:

Write the names of the .txt files:
data10.txt data20.txt data30.txt
Size of the array is...: 60 

我一直在寻找解决方案,但似乎没有任何效果。提前感谢如何解决此功能的任何提示和建议。

2 个答案:

答案 0 :(得分:2)

您可以从输入行创建istringstream并使用它来提取文件名:

//...
cin.get(str, 256);
string str2(str);
istringstream input(str2);
string filename;
while (input >> filename) {
  istream fin(filename.c_str());
  //... process as before
}

答案 1 :(得分:0)

我认为问题出在回归部分。如果是这样,您可以考虑将指针的向量返回到vector.i.e。

vector<vector<double>*> read()
相关问题