943,856 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 368
  • C++ RSS
Jun 16th, 2009
0

More Efficient way? Grabbing numbers

Expand Post »
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.
Quote ...
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?

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 7
Junior Poster
gretty is offline Offline
158 posts
since Apr 2009
Jun 16th, 2009
0

Re: More Efficient way? Grabbing numbers

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.
c++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jun 16th, 2009
0

Re: More Efficient way? Grabbing numbers

Yet another tip:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 16th, 2009
0

Re: More Efficient way? Grabbing numbers

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
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need Help with conversion
Next Thread in C++ Forum Timeline: error while compiling...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC