I am using the vector to store all the lines (parsing from a CSV file), which matched with the string that I am looking for (via string.find()!=string::npos).
But, the results didn't print out all the lines which contain the string.
CSV file:
fruit, 1000, abc, d890, 1234
fruit, 1432, abc, d890, 1234
fruit, 8923, abc, d890, 1234
fruit, 1454, abc, d890, 1234
fruit, 2574, abc, d890, 1234
fruit, 1000, abc, d890, 1234
fruit, 1000, abc, d890, 1234
water, 1000, abc, d890, 1234
water, 1000, abc, pat1, 1432
water, 1000, abc, pat2, 8923
water, 1000, abc, pat3, 1454
water, 1000, abc, pat4, 2574
water, 1000, abc, d890, 1234
The output which I want is as below:
fruit, 1432, abc, d890, 1234
fruit, 8923, abc, d890, 1234
fruit, 1454, abc, d890, 1234
fruit, 2574, abc, d890, 1234
water, 1000, abc, pat1, 1432
water, 1000, abc, pat2, 8923
water, 1000, abc, pat3, 1454
water, 1000, abc, pat4, 2574
But, actual output is
water, 1000, abc, pat1, 1432
water, 1000, abc, pat2, 8923
water, 1000, abc, pat3, 1454
water, 1000, abc, pat4, 2574
Codes as below:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <windows.h>
using namespace std;
void splitter(vector<string> HK, vector<string>& CH)
{
for(unsigned int i = 0; i < HK.size(); i++)
{
char sep = ',';
string s = HK[i];
for(size_t p = 0, q = 0; p != s.npos; p = q)
{
CH.push_back(s.substr(p + (p != 0), (q = s.find(sep, p + 1)) - p - (p != 0)));
}
}
}
int main()
{
ifstream iFile("C:\\abc.csv");
string line;
//Opening may fail, perform checking
if(!iFile)
{
cout << "Error, could not open file." << endl;
return -1;
}
string lookForStr[] = {"pat1", "pat2", "pat3", "pat4"};
//create vector to store BLT of parent and child
vector<string> lookForStrVec;
vector<string> lookForStrVecNext;
vector<string> lookForStrVecNext1;
vector<string> lookForStrVecNext11;
while(!iFile.eof())
{
getline(iFile, line);
for(unsigned int i = 0; i < 4; ++i)
{
if(line.find(lookForStr[i]) != string::npos)
{
lookForStrVec.push_back(line);
}
}
splitter(lookForStrVec, lookForStrVecNext);
for(unsigned int i = 0; i < lookForStrVecNext.size(); i += 5)
{
if(line.find(lookForStrVecNext[i + 4]) != string::npos)
{
string str = lookForStrVecNext[i + 4];
lookForStrVecNext1.push_back(str);
}
}
for(unsigned int i = 0; i < lookForStrVecNext1.size(); ++i)
{
if(line.find(lookForStrVecNext1[i]) != string::npos)
{
lookForChildStrNext11.push_back(line);
}
}
}
}
cout << endl << "print all lines with matched string..." << endl;
for(unsigned int i = 0; i < lookForStrVecNext11.size(); ++i)
{
cout << lookForChildVecNext11[i] << endl;
}
system("pause");
}