Dynamic Array, Writing to CSV, Floating Point ?'s

Reply

Join Date: Apr 2004
Posts: 4,306
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 227
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Dynamic Array, Writing to CSV, Floating Point ?'s

 
0
  #21
Aug 9th, 2005
Originally Posted by Drowzee
C doesn't explicitly support vectors, right? Otherwise, why bother using malloc at all?
No, C doesn't have vectors, which can make this problem a little easier. That's why I asked up front in regard to the design.

[edit]So sticking with that...
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <iterator>
  7. #include <cstdio>
  8.  
  9. struct sRecord
  10. {
  11. int date, volume;
  12. char time[8];
  13. double price[4];
  14. };
  15.  
  16. bool operator< (const struct sRecord &a, const struct sRecord &b)
  17. {
  18. return a.volume < b.volume;
  19. }
  20.  
  21. std::ostream& operator<< (std::ostream &o, struct sRecord &r)
  22. {
  23. return o << r.date << ","
  24. << r.time << ","
  25. << r.price[0] << ","
  26. << r.price[1] << ","
  27. << r.price[2] << ","
  28. << r.price[3] << ","
  29. << r.volume;
  30. }
  31.  
  32. int main(void)
  33. {
  34. std::vector<sRecord> record;
  35. const std::string filename("file.csv");
  36. std::ifstream file(filename.c_str());
  37. if ( file )
  38. {
  39. std::string line;
  40. while ( file >> line )
  41. {
  42. sRecord data;
  43. if ( std::sscanf(line.c_str(), "%d,%7[^,],%lf,%lf,%lf,%lf,%d",
  44. &data.date, &data.time, &data.price[0], &data.price[1],
  45. &data.price[2], &data.price[3], &data.volume) == 7 )
  46. {
  47. record.push_back(data);
  48. }
  49. }
  50. }
  51. /*
  52.   * [Modify contents]
  53.   */
  54. std::sort(record.begin(), record.end());
  55. std::copy(record.begin(), record.end(),
  56. std::ostream_iterator<sRecord>(std::cout, "\n"));
  57. return 0;
  58. }
  59.  
  60. /* file.csv
  61. 20041201,0:00,102.74,102.77,102.74,102.76,5
  62. 20041201,0:05,102.78,102.78,102.75,102.75,3
  63. 20041201,0:10,102.75,102.75,102.7,102.7,7
  64. 20041201,0:15,102.7,102.72,102.7,102.72,8
  65. 20041201,0:20,102.73,102.74,102.73,102.74,2
  66. 20041201,0:25,102.73,102.73,102.7,102.7,4
  67. 20041201,0:30,102.71,102.71,102.68,102.7,10
  68. 20041201,0:35,102.71,102.73,102.68,102.69,15
  69. 20041201,0:40,102.7,102.71,102.69,102.71,12
  70. 20041201,0:45,102.71,102.73,102.71,102.73,2
  71. 20041201,0:50,102.74,102.78,102.74,102.77,12
  72. 20041201,0:55,102.77,102.8,102.77,102.79,17
  73. */
  74.  
  75. /* my output
  76. 20041201,0:20,102.73,102.74,102.73,102.74,2
  77. 20041201,0:45,102.71,102.73,102.71,102.73,2
  78. 20041201,0:05,102.78,102.78,102.75,102.75,3
  79. 20041201,0:25,102.73,102.73,102.7,102.7,4
  80. 20041201,0:00,102.74,102.77,102.74,102.76,5
  81. 20041201,0:10,102.75,102.75,102.7,102.7,7
  82. 20041201,0:15,102.7,102.72,102.7,102.72,8
  83. 20041201,0:30,102.71,102.71,102.68,102.7,10
  84. 20041201,0:40,102.7,102.71,102.69,102.71,12
  85. 20041201,0:50,102.74,102.78,102.74,102.77,12
  86. 20041201,0:35,102.71,102.73,102.68,102.69,15
  87. 20041201,0:55,102.77,102.8,102.77,102.79,17
  88. */
I didn't know what to sort by, so I chose volume.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 12
Reputation: NolanVW is an unknown quantity at this point 
Solved Threads: 0
NolanVW NolanVW is offline Offline
Newbie Poster

Re: Dynamic Array, Writing to CSV, Floating Point ?'s

 
0
  #22
Aug 10th, 2005
I'll continue thanking you at this point...

Thank You,

Nolan
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC