Ich verwende den Vektor, um alle Zeilen zu speichern (Analyse aus einer CSV-Datei), die mit der gesuchten Zeichenfolge (über Zeichenfolge) übereinstimmt .find()! = string :: npos).string.find() kann nicht alle Zeilen ausgeben, die mit der Zeichenfolge übereinstimmen
Aber die Ergebnisse haben nicht alle Zeilen ausgedruckt, die die Zeichenfolge enthalten.
CSV-Datei:
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
Die Ausgabe, die ich will, ist wie folgt:
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
Aber tatsächliche Ausgabe ist
water, 1000, abc, pat1, 1432
water, 1000, abc, pat2, 8923
water, 1000, abc, pat3, 1454
water, 1000, abc, pat4, 2574
Codes wie folgt:
#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");
}
[eof() in während schlecht] (http: //stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) –
Siehe 'string lookForStr [] = {" pat1 "," pat2 "," pat3 "," pat4 "};' Ich erwarte nicht, dass die vier anderen übereinstimmen. –
@AndreasDM, irgendein Vorschlag? – John5012