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;
}

csv is a specialization of txt in that the text format is specific to comma separated fields with optional quoting rules for embedded commas. I'm guessing your code simply doesn't recognize the CSV format correctly. How about posting a small example of the CSV file?

Hi Narue, thanks for your response. As I mentioned in my OP, i tried to upload a small csv file but daniweb would not let me.... I can email it you however if you'd like?

It's a text file...copy paste.

attached the file, since the copy paste took a lot of space.

Wanted to point out that when i run my code on the csv file, I have no problems whatsoever. So what is confusing me is why i run into trouble with the csv file when its contents are what is in the attached txt file...

Hi Narue, thanks for your response. As I mentioned in my OP, i tried to upload a small csv file but daniweb would not let me.... I can email it you however if you'd like?

How did you get your CODE in your post? Do the same thing with your data! Sheesh!

How did you get your CODE in your post? Do the same thing with your data! Sheesh!

I attached the data in a file in the post above.

NQ01MIR 1 12/10/2009 930 1794.25 1799.5 1794 1799 1873 1797.75
NQ01MIR 1 12/10/2009 931 1799 1801 1798.5 1800.5 1004 1799.25
NQ01MIR 1 12/10/2009 932 1800.75 1801 1800 1800.75 647 1799.5
NQ01MIR 1 12/10/2009 933 1800.75 1802 1800.5 1801.5 657 1800.25
NQ01MIR 1 12/10/2009 934 1801.75 1803.25 1801.75 1802 843 1800.75
NQ01MIR 1 12/10/2009 935 1802 1803.75 1801.25 1803.25 1084 1802
NQ01MIR 1 12/10/2009 936 1803.25 1806.75 1803.25 1806 1308 1804.75
NQ01MIR 1 12/10/2009 937 1806 1806 1804.5 1805 755 1803.75
NQ01MIR 1 12/10/2009 938 1805 1805.75 1803.75 1803.75 481 1802.5
NQ01MIR 1 12/10/2009 939 1803.75 1805.5 1803.25 1804 1038 1802.75
NQ01MIR 1 12/10/2009 940 1804 1805.5 1804 1805 1049 1803.75
NQ01MIR 1 12/10/2009 941 1805 1805 1804 1804.25 375 1803
NQ01MIR 1 12/10/2009 942 1804 1806.25 1803.75 1805.25 1323 1804
NQ01MIR 1 12/10/2009 943 1805.25 1805.75 1805.25 1805.5 375 1804.25
NQ01MIR 1 12/10/2009 944 1805.25 1805.5 1804.75 1805 511 1803.75
NQ01MIR 1 12/10/2009 945 1805 1805.75 1804.5 1804.5 871 1803.25
NQ01MIR 1 12/10/2009 946 1804.5 1805.5 1803.75 1804 1104 1802.75
NQ01MIR 1 12/10/2009 947 1804 1804.75 1803.75 1804.5 364 1803.25
NQ01MIR 1 12/10/2009 948 1804.75 1805.25 1803.75 1804 429 1802.75
NQ01MIR 1 12/10/2009 949 1804 1804.5 1803.75 1804 147 1802.75
NQ01MIR 1 12/10/2009 950 1804 1804 1802 1802.25 761 1801
NQ01MIR 1 12/10/2009 951 1802.5 1802.5 1801 1801 393 1799.75
NQ01MIR 1 12/10/2009 952 1801.25 1802 1800.5 1802 350 1800.75
NQ01MIR 1 12/10/2009 953 1802 1802 1799 1799.5 495 1798.25
NQ01MIR 1 12/10/2009 954 1799.5 1800.75 1799.25 1799.75 434 1798.5
NQ01MIR 1 12/10/2009 955 1800 1801.25 1800 1800.75 450 1799.5
NQ01MIR 1 12/10/2009 956 1800.75 1800.75 1799.75 1799.75 128 1798.5
NQ01MIR 1 12/10/2009 957 1799.5 1801 1799.5 1800.5 444 1799.25
NQ01MIR 1 12/10/2009 958 1800.5 1802 1800.5 1801.75 270 1800.5
NQ01MIR 1 12/10/2009 959 1801.75 1802.25 1799.75 1799.75 129 1798.5
NQ01MIR 1 12/10/2009 1000 1800 1800.75 1799.75 1800 270 1798.75

Member Avatar for MonsieurPointer

Question: Are you positive that there are actually tabs between the data columns and not just n spaces?

Question: Are you positive that there are actually tabs between the data columns and not just n spaces?

Salut Monsieur,

I opened up the price data file (attached in post above) and I see a tab between the date and time field, but then it looks like n space between the other fields. (the first and last field are ignored in the program). If that's the problem, how does one go about skipping white spaces of variable length?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.