EmilDo
EmilDo

Reputation: 1177

Reading multiple .txt files into a vector<double>

Good Day,

I have written the following function that is part of some calculations:

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;


}  

This function reads single .txt file with numbers and saves them to vector that is returned for further calculations in another functions.

The function works fine, but I want to edit it to work with multiple .txt files that are saved, an example of this is:

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

I have been looking for solution the whole day but nothing seems to work. Thanks in advance for any tips and suggestions how to solve this function.

Upvotes: 0

Views: 179

Answers (2)

deeiip
deeiip

Reputation: 3379

I think the problem is in the returning part. If it is so you may consider returning of vector of a pointer to vector.i.e.

vector<vector<double>*> read()

Upvotes: 0

You could create a istringstream from the input line and use it to extract file names:

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

Upvotes: 2

Related Questions