I need help with adding a few lines to this code to be able to search through a
ID: 3556295 • Letter: I
Question
I need help with adding a few lines to this code to be able to search through a .csv database of over 100 thousand entries to find a match. It needs to list some of the information from the row of the matched object if there is a match. The output of the search needs to cout the data in column B C D and E as its listed in Excell. Also cout the time of how long it took for the search to be performed.
Each of the 100 thousand entries in the database has 6 columns A B C D E F but the matched search output only needs to list column B C D and E as the search output. I can provide the .csv file if it'll help.
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
struct List
{
string data1;
string data2;
string data3;
string data4;
};
vector split(string &s, char delim)
{
vector elems;
string item;
stringstream ss(s);
while (getline(ss, item, delim))
if(!item.empty())
elems.push_back(item);
return elems;
}
int listone(){
string line;
string date, domain, ip, rev_lookup, desc, reg;
ifstream myfile;
int list_iter = 0;
cout << "Database loading." <
myfile.open ("database.csv");
List *item = new List[88000];
while(getline(myfile, line)){
}
myfile.close();
cout << " DataBase loaded!" << endl;
return 0;
}
string getUSerInput(){
string userInput;
cout << "Please enter what you want to check: " <
cin >> userInput;
return userInput;
}
int checkInfections(){
return 0;
}
int main(int argc, char *argv[])
{
listone();
system("PAUSE");
return EXIT_SUCCESS;
}
Explanation / Answer
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
struct List
{
string data1;
string data2;
string data3;
string data4;
string data5;
string data6;
};
vector<string> split(string &s, char delim)
{
vector<string> elems;
string item;
stringstream ss(s);
while (getline(ss, item, delim))
if(!item.empty())
elems.push_back(item);
return elems;
}
int listone(string site){
string line;
string date, domain, ip, rev_lookup, desc, reg;
ifstream myfile;
int list_iter = 0;
cout << "Database loading." <<endl;
myfile.open ("database.csv");
List *item = new List[88000];
while(getline(myfile, line)){
vector<string> elems = split(line,',');
if(site == elems[1]){
int i;
for(i=1;i<=4;i++){
cout<<elems[i]<<" ";
}
cout<<endl;
}
}
myfile.close();
cout << " DataBase loaded!" << endl;
return 0;
}
string getUserInput(){
string userInput;
cout << "Please enter what you want to check: " <
cin >> userInput;
return userInput;
}
int checkInfections(){
return 0;
}
int main(int argc, char *argv[])
{
string site = getUserInput();
listone(site);
return EXIT_SUCCESS;
}
//Hope this helps
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.