More Efficient way? Grabbing numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 147
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

More Efficient way? Grabbing numbers

 
0
  #1
Jun 16th, 2009
Hi

I am reading a excel file & grabbing certain elements out of the file to store in arrays. I know how to do it, but can you tell me if there is a more efficient way to do this. This, meaning, grabbing certain elements out of a line/string.

Inside the excel the information is arrange like so. And I am grabbing the numbers in bold in each line.
AED,0.915,0.005,0.910,0.920,0.910,0.925,0.910,318870,,76,70,0
AAC,1.520,-0.030,1.520,1.525,1.540,1.540,1.520,459155,,45,68,-1
AWL,0.003,-0.001,0.003,0.004,0.004,0.004,0.003,989216,,17500,53,-18
..........
My way of getting the information is like so, but it seems really amateurish to me . Is there a better way to do it?

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. struct stocks
  9. {
  10. string stock_data[999];
  11. string code[999];
  12. float price[999];
  13. float purchase[999];
  14. float quantity[999];
  15. };
  16.  
  17. int main()
  18. {
  19.  
  20. stocks current_data;
  21. int i = 0;
  22.  
  23. ifstream infile;
  24.  
  25. infile.open("Watchlists.csv");
  26.  
  27. if (!infile)
  28. {
  29. cout << "Failed to open file";
  30. return 0;
  31. }
  32.  
  33.  
  34. while(infile)
  35. {
  36. getline(infile, current_data.code[i], ',');
  37. infile.ignore(4000, ',');
  38. infile.ignore(4000, ',');
  39. infile.ignore(4000, ',');
  40. infile >> current_data.price[i];
  41. infile.ignore(4000, ',');
  42. infile.ignore(4000, ',');
  43. infile.ignore(4000, ',');
  44. infile.ignore(4000, ',');
  45. infile.ignore(4000, ',');
  46. infile.ignore(4000, ',');
  47. infile >> current_data.quantity[i];
  48. infile.ignore(4000, ',');
  49. infile.ignore(4000, ',');
  50. infile.ignore(4000, '\n');
  51. i++;
  52.  
  53. }
  54.  
  55. return 0;
  56. }
Last edited by gretty; Jun 16th, 2009 at 1:59 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: More Efficient way? Grabbing numbers

 
0
  #2
Jun 16th, 2009
Do you mean efficient, or elegant?

Do you mean efficient, or adaptable?

You could create (or find, there are plenty for this common exercise) a CSV class which does all the comma parsing for you.
  1. while ( line.getline() ) {
  2. CSV parser(line); // does all the magic of splitting a line at commas
  3. current_data.code[i] = parser.column(1); // and so on
  4. }

Oh, and it's probably more useful to make an array of your struct, not have arrays inside your struct.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: More Efficient way? Grabbing numbers

 
0
  #3
Jun 16th, 2009
Yet another tip:
  1. while (infile) // wrong loop!
  2. {
  3. getline(...);
  4. ... >> ...
  5. i++
  6. }
See what happens if the file is empty or after the last line reading:
1. infile is good so the next loop entered...
2. getline failed, all subsequent input operations are suppressed...
3. operator >> gets nothing...
4. i++ counts inexistent record...

See the simplest tokenizer in my recent post:
http://www.daniweb.com/forums/thread197527.html
Use correct loop conditions and always test every input operation result.
For example:
  1. ...
  2. vector<string> v;
  3. ...
  4. for (i = 0; getline(is,t); i++) { // you have the next record!
  5. Tokenize(t,v,',');
  6. if (v.size() >= number_of_fields) {
  7. // get selected fields values
  8. }
  9. }
I agree with Salem: as usually, more efficient means bla-bla-bla - it's an unclear ambiguous term until you define an efficiency criterion.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: More Efficient way? Grabbing numbers

 
0
  #4
Jun 16th, 2009
And yet another question to OP:
Why you are starting a new thread on the same problem?
Orphaned thread:
http://www.daniweb.com/forums/thread197527.html
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC