943,696 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 7541
  • C RSS
You are currently viewing page 3 of this multi-page discussion thread; Jump to the first page
Aug 9th, 2005
0

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

Quote 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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 10th, 2005
0

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

I'll continue thanking you at this point...

Thank You,

Nolan
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NolanVW is offline Offline
12 posts
since Jun 2005

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: graphices header file
Next Thread in C Forum Timeline: Pointer question





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


Follow us on Twitter


© 2011 DaniWeb® LLC