I\'m trying to create a code that will parse through a csv database with stock information. Currently, I have the code generated so that it will search with a keyword and pr
Here it is your solution (the most close to what you requested):
#include
#include
#include
#include
#include
using namespace std;
typedef std::vector record;
std::istream&
operator>>(
std::istream& is,
record& r)
{
r.clear();
string line;
getline(is, line);
istringstream iss(line);
string field;
while (getline(iss, field, ',' ))
r.push_back(field);
return is;
}
int
main()
{
ifstream file("Stock Database.csv");
record headers, r;
if (file.good())
file >> headers;
while (file >> r)
{
for (int i = 0; i < r.size(); i++)
cout << headers[i] << ":\t" << r[i] << endl;
cout << "------------------------------" << endl;
}
return 0;
}
// EOF
The content of the "Stock Database.csv" file is:
Symbol,Name,Price,High Today,Low Today,52 Week Low
GOOG,Google Inc.,$568.77 ,$570.25 ,$560.35
AAPL,Apple Inc.,$93.28 ,$63.89 ,$99.44
Just do with the record whatever you want. The first read from the file suppose to bring you headers. Every next read fill each record with csv values.