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 */
| |