Creating a matrix from an input file

Dear C++ users,

I am very new in C++. I have a question concerning reading an input file, where I have to create a matrix from same elements of the data if a condition is fullfilled.

Here is my input files (matrix.txt)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
This is a line, below is another line with numbers
0.15 10.1

*M
2.5  3.01  4.11
*M
6.16  7.17  8.01
*M
10.16 11.133 12.12
*M
14.17 15.110 16.16
Reading should stop in this line
another line
another line
15.0  12.015 13.001



And my code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Reading the input file.
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <limits>
#include <iterator>
#include <stdlib.h>
using namespace std;

vector<vector<double> > read(ifstream& fil, string sec,string fir)
{
    string lines;
    vector<vector<double> > data;

        if (fir == sec)
        {
            while (getline(fil,lines))
            {
                istringstream ss(lines);
                //stringstream is(lines);
                data.push_back(vector<double>(istream_iterator<double>(ss),
                                              istream_iterator<double>()));
            }
        }
    return data;
}

int main()
{
    int n, m, c;
    ifstream pa("matrix.txt");

    vector<vector<double> > matr;
    string first, ba("*M");

    while (pa>>first)
    {
        pa.ignore(numeric_limits <streamsize>::max(),'\n');
        matr = read(pa,ba,first);

    }
    n = matr.size();
    m = matr[0].size();


    for ( int i = 0; i<n; i = i+2)
    {
        for (int j = 0; j<m; j++)
        {
            cout << matr[i][j]<< "  ";
        }
        cout << '\n';
    }
    cout << n << '\n';
    cout << m;
    return 0;
}


From input file I should create a matrix as follow:

[2.5 3.01 4.11
6.16 7.17 8.01
10.16 11.133 12.12
14.17 15.110 16.16]

The question is how can I read only those lines after each line of *M? Currently the code reads all lines after the first *M.

Thank you very much in advance!
Last edited on
Since you know it's 4 lines of numbers. Couldnt you use a for-loop that runs 4 times and reads in all the numbers, that way it will stop where you want it to, no?
Actually, lines of input file can change. It could be more or less, and I am interested only on those lines after each *M.
Well then, you can first read the file word by word, using std::cin instead of std::getline. If word is word is ever equal to *M you know that the next line will be numbers. So after you've read 14.17 15.110 16.16 and attempt to read in the next word, if it's not *M, then ignore it. Does that make sense?
Dear TarikNeaj,

Thank you for suggestions (especially learning on how to debug was very usefull), now everthing is working fine.
Glad it worked out =) Would you mind posting the full working program So other people that might come across this thread can get help from it?
Dear TarikNeaj,

Sure I can post the code.
I slightly changed input file (by adding & after each row). Here is the new input file with and the code.
Have a great weekend!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
This is a line, below is another line with numbers
0.15 10.1

*M
2.5  3.01  4.11
&
*M
6.16  7.17  8.01
&
*M
10.16 11.133 12.12
&
*M
14.17 15.110 16.16
Reading should stop in this line
another line
another line
15.0  12.015 13.001


The code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Reading the input file.
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <limits>
#include <iterator>
#include <cmath>
using namespace std;

vector<vector<double> > read(ifstream& fil, string sec,string fir,string last)
{
    string lines;
    vector<vector<double> > data;

        while (fil >> fir)
        {
            fil.ignore(numeric_limits <streamsize>::max(),'\n');
            if (fir == sec)
            {
                while (fil>>lines)
                {
                    if (lines == "&") break;
                    if (lines == last) break;
                    data.push_back(vector<double>());
                    stringstream is(lines);
                    double nm;
                    while (is >> nm)
                    {
                        data.back().push_back(nm);
                    }


                }
            }
        }

    return data;
}


int main()
{
    int n, m;
    ifstream pa("matrix.txt");

    vector<vector<double> > matr, ma;
    string first, ba("*M");
    string fo("Reading");

    matr = read(pa,ba,first,fo);

    n = matr.size();
    m = matr[0].size();

    for ( int i = 0; i<n; i =i+1)
    {
        for (int j = 0; j<m; j++)
        {

                cout << matr[i][j]<< "  ";
        }
        //cout << '\n';
    }

    cout << '\n'<<n << '\n';
    cout << m;
    return 0;
}
Topic archived. No new replies allowed.