Hi everyone,
When I try the code below with data in a txt file, I have no problems and the data prints properly and neatly on the screen. (Note, one slight modification when using a txt file, instead of getline (ist, ignored, ','), I do getline (ist, ignored, '/t') to ignore tabs since the txt file is tab delimited )
But when I try the code below on a csv file, it ends up printing a jumbled mess and keeps on in a never ending loop, even though the data is limited to a few days worth of data.
I tried uploading my csv file, but daniweb doesn't let me do that.
Thanks for any pointers,
TR
#include <stdafx.h>
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>
struct PriceInfo
{
double Open;
double High;
double Low;
double Close;
unsigned int Volume;
unsigned int Time;
std::string Date;
};
typedef std::vector<PriceInfo> PriceList;
std::istream& operator>>(std::istream& ist, PriceInfo& priceInfo)
{
std::string ignored;
std::string line;
getline (ist, ignored, ',');
getline (ist, ignored, ',');
getline (ist, priceInfo.Date, ',');
getline (ist, line);
std::istringstream linestream(line);
linestream >> priceInfo.Time >> priceInfo.Open >> priceInfo.High >> priceInfo.Low >> priceInfo.Close >> priceInfo.Volume;
return ist;
}
std::ostream& WriteAmount(std::ostream& ostream,
double amount)
{
ostream
<< std::setw(10) << std::right
<< std::fixed << std::setprecision(2)
<< amount;
return ostream;
}
std::ostream& operator<<(std::ostream& ostream,
const PriceInfo& priceInfo)
{
ostream << "| " << std::setw(10) << priceInfo.Date;
WriteAmount(ostream, priceInfo.Time);
WriteAmount(ostream, priceInfo.Open);
WriteAmount(ostream, priceInfo.High);
WriteAmount(ostream, priceInfo.Low);
WriteAmount(ostream, priceInfo.Close);
WriteAmount(ostream, priceInfo.Volume);
ostream << " |";
return ostream;
}
template <typename T>
std::istream& operator>>(std::istream& ist,
std::vector<T>& vector)
{
std::copy(
std::istream_iterator<T>(ist),
std::istream_iterator<T>(),
std::back_inserter(vector));
return ist;
}
template <typename T>
std::ostream& operator<<(std::ostream& ostream,
const std::vector<T>& vector)
{
std::copy(
vector.begin(),
vector.end(),
std::ostream_iterator<T>(ostream, "\n"));
return ostream;
}
void ReadPricesFromFile(const char* filename, std::vector<PriceInfo>& prices)
{
std::ifstream input(filename);
input >> prices;
}
void PrintPrices(std::ostream& output, const PriceList& prices)
{
output << "| "
<< std::setw(10) << "Date" << " | "
<< std::setw(6) << "Time" << " | "
<< std::setw(6) << "Volume" << " | "
<< std::setw(8) << "Open" << " | "
<< std::setw(8) << "High" << " | "
<< std::setw(8) << "Low" << " | "
<< std::setw(8) << "Close" << " |"
<< std::endl << prices;
}
int main(int argc, char* argv[])
{
if (argc != 2)
{
std::cerr << "Usage: " << argv[0] << " filename " << std::endl;
return EXIT_FAILURE;
}
PriceList prices;
ReadPricesFromFile(argv[1], prices);
PrintPrices(std::cout, prices);
return EXIT_SUCCESS;
}