Nested parsing vector

Just learning C++, need to do a "Proof of concept" for signal data manipulation.

Have a series of csv files that are 4 columns wide, 100,000s of rows high (~30MB each). I need to split the data into individual 2d vectors (originally considered arrays, but size may change with input file) based on which column the data resides and only if the data in column 2 is <-.5, example:

0.01, -0.2, 1.3, 2.4 // ignored due to position 2 data
0.02, -0.6, 1.4, 2.5 // valid data, 3 and 4 to their own vectors
0.03, -0.6, 1.5, 2.6 // valid data, add 3 and 4 to their own vectors
0.04, -0.4, 1.6, 2.7 // ignored due to position 2 data
0.05, -0.6, 1.7, 2.8 // valid data, 3 and 4 to new columns on vectors
0.06, -0.6, 1.8, 2.9 // valid data, add 3 and 4 to their own vectors

Would become two vectors:
1)
1.4 1.7
1.5 1.8

2)
2.5 2.8
2.6 2.9

Did enough reading on introductory to C++ and found pieces of code on line to get csv into a single 2d vector, but not sure how to go about parsing or sub routines.

Any thoughts appreciated.
Last edited on
Following program checked on a Notepad file saved in .csv format, Windows 8. Comments within program, shout if anything's unclear
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <utility>

int main()
{
    std::vector<std::vector<double>> fileNumbers{};
    std::ifstream inFile{"C:\\comma_file.csv"};
    if(inFile)
    {
        std::string line{};
        while (getline(inFile, line))//first read the whole line into string
        {
            std::istringstream stream{line};//create istringstream object with the string
            std::string numOneStr{}, numTwoStr{}, numThreeStr{};//temp holding strings
            double numFour{};//fourth number can be read off directly
            if(stream)
            {
                getline(stream, numOneStr, ',')&&//short circuit evaluation with delimiter version of getline()
                    getline(stream, numTwoStr, ',')&&
                        getline(stream, numThreeStr, ',')&&
                            stream >> numFour;
            }
            std::istringstream numTwoStream{numTwoStr};//to check the second number
            double numTwo{};
            numTwoStream >> numTwo;
            if(numTwo < -0.5)
            {
                std::istringstream numThreeStream{numThreeStr};//extract third number only if second is < -0.5
                double numThree{};
                numThreeStream >> numThree;

                if (inFile)//double-check file is still open to avoid spurious extra reads at end
                {
                    std::vector<double> temp{numThree, numFour};
                    fileNumbers.push_back(std::move(temp));
                    //http://en.cppreference.com/w/cpp/container/vector/push_back
                    temp.clear();
                }
            }
        }
    }
    for (const auto& elemOuter : fileNumbers)//range-loops, C++11
    {
        for (const auto& elemInner : elemOuter)
        {
            std::cout << elemInner << " ";
        }
        std::cout << "\n";
    }
}
/* test file: 
0.01, -0.2, 1.3, 2.4 
0.02, -0.6, 1.4, 2.5 
0.03, -0.6, 1.5, 2.6 
0.04, -0.4, 1.6, 2.7 
0.05, -0.6, 1.7, 2.8 
0.06, -0.6, 1.8, 2.9 */
My test run output two lines, likely delimiter difference between test files. I'll go through code and tweak it a bit.

This gives me a good start. Exactly what I was looking for.

Much thanks.

Topic archived. No new replies allowed.