stringstream getline() only reading first line of cells

I want the following code to print all the cells of a comma separated values (csv) file.
It only prints the first row of cells.
Why is it not printing cells of the remaining rows?

Thank you.

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

class CSVMatrix
{
    public:
        void loadMatrix(std::istream& inStream)
        {
            std::string         line;
            std::stringstream   lineStream;
            std::string         cell;

            int row=0;

            //read lines
            while( std::getline(inStream, line) )
            {
                std::cout << "row=" << row++ << " line=" << line << std::endl;
                lineStream << line;

                //read cells
                while(std::getline(lineStream, cell, ','))
                {
                    std::cout << "cell=" << cell << std::endl;
                }
            }
        }
};

int main()
{
    std::ifstream       infile("plop.csv");
    CSVMatrix           matrix;

    matrix.loadMatrix(infile);
}


input file plop.csv:
1
2
3
4
1,2,3,4,5,
a,b,c,d,e
m,n,o,p,q,
u,v,wz, x ,y,


output:
1
2
3
4
5
6
7
8
9
row=0 line=1,2,3,4,5,
cell=1
cell=2
cell=3
cell=4
cell=5
row=1 line=a,b,c,d,e
row=2 line=m,n,o,p,q,
row=3 line=u,v,wz, x ,y,
After the while loop at line 24 has completed, the lineStream will have a fail status.
Before the next use of that stream, reset the flags:
 
lineStream.clear();


Also, it may not matter much here, but the size of that stringstream is gradually increasing,
try
 
    std::cout << "lineStream.str() = " << lineStream.str() << '\n';

to see the contents.

To make it contain just the current line, you could use
 
    lineStream.str(line);
instead of
 
    lineStream << line;
because the error flag of lineStream is set after the first line.

try replace lineStream << line
with
1
2
lineString.clear();
lineString.str() = line;
Alternatively, check for eof() and exit the loop if true.
1
2
3
4
5
while( std::getline( lineStream, cell, ',' ) ) {
    if( lineStream.eof( ) ) break;

    std::cout << "cell=" << cell << '\n';
}
Alternatively, check for eof() and exit the loop if true.

As an alternative to what?
Thank you folks, that worked great:

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
#include <iterator>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

class CSVMatrix
{
    public:
        void loadMatrix(std::istream& inStream)
        {
            std::string         line;
            std::stringstream   lineStream;
            std::string         cell;

            int row=0;

            //read lines
            while( std::getline(inStream, line) )
            {
                lineStream.clear();
                lineStream.str(line);
                std::cout << "row=" << row++
                    << " lineStream.str() = " << lineStream.str() << std::endl;

                //read cells
                while(std::getline(lineStream, cell, ','))
                {
                    std::cout << "cell=" << cell << std::endl;
                }
            }
        }
};

int main()
{
    std::ifstream       infile("plop.csv");
    CSVMatrix           matrix;

    matrix.loadMatrix(infile);
}


output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
row=0 lineStream.str() = 1,2,3,4,5,
cell=1
cell=2
cell=3
cell=4
cell=5
row=1 lineStream.str() = a,b,c,d,e
cell=a
cell=b
cell=c
cell=d
cell=e
row=2 lineStream.str() = m,n,o,p,q,
cell=m
cell=n
cell=o
cell=p
cell=q
row=3 lineStream.str() = u,v,wz, x ,y,
cell=u
cell=v
cell=wz
cell= x 
cell=y
Topic archived. No new replies allowed.