2

What i want to do: Fill vectors with data from .txt file which contains 3 columns of unknown length. Each column for each vector. I also want to print vectors on screen.

Here is the code:

using namespace std;
class Equation
{
    vector < vector <double> > u;
    vector< vector <double> > alfa;
    vector < vector <double> > f;
    string fileName;

    public:
    Equation(string fileName);

};
Equation::Equation(string fileName)
{
    cout<< "Give name of the file:" << endl;
    cin >> fileName;

    fstream plik;
    plik.open( fileName.c_str(), ios::in | ios::out );

    if( plik.good() == true )
    {
        cout << "file is open" << endl; 
        bool first = false;
        if (plik) 
        {
            string line;
            while (getline(plik,line)) 
            {
                istringstream split(line);
                double value;
                int col = 0;
                char sep;

                double dummy;
                while (split >> value >> dummy )
                {
                    if(!first)
                    {
                        // Each new value read on line 1 should create a new inner vector
                        u.push_back(vector<double>(value));
                    }
                    u[col].push_back(value);
                    ++col;
                    // read past the separator                
                    split >> sep;
                }
            // Finished reading line 1 and creating as many inner
            // vectors as required            
            first = true;
            }
        }
        // printing content of vector u:
        for (const auto & vec : u) 
        {
            for (const auto elem : vec)
            cout  << elem << ' ';
            cout << '\n';
        }
        // now vector alfa: 
        bool second = false;
        if (plik) 
        {
            string sline; //as second line
            while (getline(plik,sline)) 
            {
                istringstream ssplit(sline); // as second split
                double svalue;
                int scol = 0;
                char ssep;

                while ( ssplit  >> svalue)
                {
                    if(!second)
                    {
                        // Each new value read on line 1 should create a new inner vector
                        alfa.push_back(vector<double>());
                    }
                    alfa[scol].push_back(svalue);
                    ++scol;
                    // read past the separator                
                    ssplit>>ssep;
                }
                // Finished reading line 1 and creating as many inner
                // vectors as required            
                second = true;
            }
        }
        for (const auto & svec : alfa) 
        {
            for (const auto selem : svec)
                cout << "wektor u: " << selem << ' ';
            cout << '\n';
        }
    }
    else 
        cout << "error" << endl;    
}

int main()
{
    Equation("");
    getch();
    return( 0 );
}

What are the problems:

  1. I puted 'dummy' variable since without it my output was vector filled with whole file, but i don't know if thats proper solution. Now my output is vector u filled with values from first column.
  2. How to fill other vectors with values from second and third column?
  3. How to handle the fact that length of the columns (file) is unknown and may be any?
  4. Would someone explain how compiler knows when to stop creating and filling inner vectors? Variable 'col' stands for that, but how exactly does it work?
JeJo
  • 20,530
  • 5
  • 29
  • 68
Uroboros
  • 39
  • 5
  • It may be helpful if you give an example of the input and the desired output. – mxdg Aug 09 '18 at 20:52
  • @mxdg Input and output are the same since i'm supposed to store vectors of data in this class. I just wanna be sure that i'm filling them properly. Example file is just 3 columns of doubles separated by blank spaces. – Uroboros Aug 09 '18 at 21:55

1 Answers1

1

If I get you right, this is an example of a legal .txt file:

info.txt

125.5 1648.2 16325.151
156464.1631 1231.156 0.1
1 231 54832
15236.15 12 0.6414787

In cpp, you can read a file by it structure with fit variables in your code. Try the following code:

struct DataReader {
    vector<double> a_vec;
    vector<double> b_vec;
    vector<double> c_vec;
};

int main(){

    ifstream file("/path/to/your/file/info.txt");
    DataReader dr;

    double a, b, c;
    while (!file.eof()) {
        file >> a >> b >> c;
        dr.a_vec.push_back(a);
        dr.b_vec.push_back(b);
        dr.c_vec.push_back(c);
    }

    for (size_t i = 0; i < dr.a_vec.size(); i++) {
        cout << dr.a_vec[i] << " " << dr.b_vec[i] << " " << dr.c_vec[i] << endl;
    }

    return 0;
}
CoralK
  • 3,341
  • 2
  • 8
  • 21
  • @KoreIK Why is there 'size_t' type in a 'for' loop? I've read about it but not quite understood. It has sth to do with the fact that length of the file is unknown? Also, thanks a lot for such simple answer! – Uroboros Aug 09 '18 at 21:42
  • @Uroboros `size_t` is a type for `unsigned int`, what means that this variable can't get negative values. The `loop` will work the same way in this case, with `int i`, but there are alot of numbers that `int` type can get, that are meanless in case of `vector` length. `size_t` get get much larger numbers then `int`, and in very large vectors it can cause that you will can't reach some of the places in the `vector`. With `size_t` you don't have this problem, because `dr.a_vec.size()` returning an `size_t` type value. – CoralK Aug 09 '18 at 21:50
  • using eof() in a loop condition is probably wrong: https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Jerry Jeremiah Aug 10 '18 at 01:43