Work Problem

Hello,
I am fairly new at C++ and I need to write a program in C++ that opens a csv file, parse the sections into arrays or vectors, and be able to output the arrays or vectors to the screen. This is a large file with over 65k rows.
I want to call the arrays "Cusip", "Time", "Ticker", "Exchange", "Shares", "Price", "Direction", "Code"
The sample data is as follows:
12C51CV12, 20140612 13:30:48.162, BRK/B, NSDQ, 1500, 65.35, SELL, B
All help is needed and appreciated.
to read the csv
1
2
3
4
5
6
7
8
9
//std::ifstream input("some_file_name");
std::string line;
while( std::getline(input, line, '\n') ){
   std::stringstream row(std::move(line));
   std::string field;
   while( std::getline(row>>std::ws, field, ',') ){
      //here 'field' has one field (section)
   }
}

There every section gets stored in one string, parse that later to create the appropriate type.

You say that you want several arrays, if the data in the row is related perhaps you could have just one array of an struct (that encapsulates and manages such data)
Last edited on
Thank you ne555. How would I do a struct that I can then output each field? I want to be able to sum the 'column' Shares and be able to do calculations such as multiply the 'column' Shares *Price and total that. Thanks
maybe try excel nor openoffice.

If you are using VS.Net you could try to import excel library then put data in database (MySQL/SQLite) then work dirrectly on these data and then show them with database controls like datagrids.

Else if you are on GCC nor MinGW it could be hardest cause no window classes are present, except, of course, winAPI nor Glade nor GTK, but it need some time to learn it.

After you can do a console interaction but it's not realy user friendly nand developper friendly.

Have a nice work.
1
2
3
4
5
6
7
8
struct item{
   double shares;
   money price; //you need precision when working with money

   money total() const{
      return shares*price;
   }
};


If you want to work with the columns you may use std::valarray
Then the operations would be
1
2
3
4
5
std::valarray<double> shares; //fill it
std::valarray<money> price;
std::valarray<money> total = shares*price; //multiply the columns
std::cout << total.sum() << '\n';
shares.sum(); //sum the shares column 
So far I have:

std::ifstream input("random.csv");
std::string line;
while( std::getline(input, line, '\n') ){
std::stringstream row(std::move(line));
std::string columns;
while( std::getline(row>>std::ws, columns, ',') ){


How do I assign each value from the csv file into a column parsed out by the comma?
Then can I output what is in each column to the screen?
Data is as follows in csv file:
12C51CV12, 20140612 13:30:48.162, BRK/B, NSDQ, 1500, 65.35, SELL, B
http://stackoverflow.com/q/236129/1959975

By the way, please don't ask for help in private like you have done.
Last edited on
Work problem
I need to write a program in C++ that ...

Why C++? There are other options, like: https://docs.python.org/2/library/csv.html
Topic archived. No new replies allowed.