| | |
Dynamic Array, Writing to CSV, Floating Point ?'s
![]() |
•
•
•
•
Originally Posted by Drowzee
C doesn't explicitly support vectors, right? Otherwise, why bother using malloc at all?
[edit]So sticking with that...
C Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> #include <vector> #include <algorithm> #include <iterator> #include <cstdio> struct sRecord { int date, volume; char time[8]; double price[4]; }; bool operator< (const struct sRecord &a, const struct sRecord &b) { return a.volume < b.volume; } std::ostream& operator<< (std::ostream &o, struct sRecord &r) { return o << r.date << "," << r.time << "," << r.price[0] << "," << r.price[1] << "," << r.price[2] << "," << r.price[3] << "," << r.volume; } int main(void) { std::vector<sRecord> record; const std::string filename("file.csv"); std::ifstream file(filename.c_str()); if ( file ) { std::string line; while ( file >> line ) { sRecord data; if ( std::sscanf(line.c_str(), "%d,%7[^,],%lf,%lf,%lf,%lf,%d", &data.date, &data.time, &data.price[0], &data.price[1], &data.price[2], &data.price[3], &data.volume) == 7 ) { record.push_back(data); } } } /* * [Modify contents] */ std::sort(record.begin(), record.end()); std::copy(record.begin(), record.end(), std::ostream_iterator<sRecord>(std::cout, "\n")); return 0; } /* file.csv 20041201,0:00,102.74,102.77,102.74,102.76,5 20041201,0:05,102.78,102.78,102.75,102.75,3 20041201,0:10,102.75,102.75,102.7,102.7,7 20041201,0:15,102.7,102.72,102.7,102.72,8 20041201,0:20,102.73,102.74,102.73,102.74,2 20041201,0:25,102.73,102.73,102.7,102.7,4 20041201,0:30,102.71,102.71,102.68,102.7,10 20041201,0:35,102.71,102.73,102.68,102.69,15 20041201,0:40,102.7,102.71,102.69,102.71,12 20041201,0:45,102.71,102.73,102.71,102.73,2 20041201,0:50,102.74,102.78,102.74,102.77,12 20041201,0:55,102.77,102.8,102.77,102.79,17 */ /* my output 20041201,0:20,102.73,102.74,102.73,102.74,2 20041201,0:45,102.71,102.73,102.71,102.73,2 20041201,0:05,102.78,102.78,102.75,102.75,3 20041201,0:25,102.73,102.73,102.7,102.7,4 20041201,0:00,102.74,102.77,102.74,102.76,5 20041201,0:10,102.75,102.75,102.7,102.7,7 20041201,0:15,102.7,102.72,102.7,102.72,8 20041201,0:30,102.71,102.71,102.68,102.7,10 20041201,0:40,102.7,102.71,102.69,102.71,12 20041201,0:50,102.74,102.78,102.74,102.77,12 20041201,0:35,102.71,102.73,102.68,102.69,15 20041201,0:55,102.77,102.8,102.77,102.79,17 */
"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
![]() |
Other Threads in the C Forum
- Previous Thread: graphices header file
- Next Thread: Pointer question
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory feet fflush fgets file floatingpointvalidation fork forloop frequency givemetehcodez grade gtkgcurlcompiling gtkwinlinux hacking highest histogram inches input intmain() iso kernel keyboard kilometer km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match microsoft mqqueue mysql number oddnumber odf opendocumentformat openwebfoundation owf pattern pdf performance posix probleminc process program programming radix recv recvblocked repetition research reversing scanf scheduling segmentationfault sequential single socket socketprograming socketprogramming stack standard string systemcall threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






