Aske
Lederhosen
Karma: 120 Posts: 31405
Offline
|
|
c++ gurus? - read in arbitrary csv...
« on: April 26, 2007, 07:23:37 PM » |
|
I used to be ok with c++ ... I'm still moderately ok (just browsed an old book)...
i remember strongly how to work with arrays, ints, do math, write function calls , etc
i remember diddly squat about how to work with strings (hence, how to adequately treat the stream that fget or fscanf takes in, etc) ....
so...
ANYONE have handy a good generic csv reader that will take arbitrary csv file and read it into an array on the fly, each whitespace or comma (and obviously line ends) creating new array elements?
I am sure I can write it ... but I'd rather save debugging time...
|
|
|
Logged
|
Russia has invaded a sovereign neighboring state and threatens a democratic government elected by its people. Such an action is unacceptable in the 21st century. -- Chimpy McFlightsuit, CEO of Bu$hco Industries of 'Merka
|
|
|
Uisce Beatha
Amazing Technicolor Dreamcoat From: In the Jar
Karma: 116 Posts: 7357
OfflineGet me the tank!
|
|
Re: c++ gurus? - read in arbitrary csv...
« Reply #1 on: April 26, 2007, 07:35:05 PM » |
|
It's been a million years and the last time I had C++ code laying around it was probably on a 286. But... strings are just arrays of characters. If you're comfortable with arrays just str.copy(buf, i) and go crazy. But you'll have to debug. Man, that was ages ago. I'm old. edit: If you're gonna break on whitespace it's technically not CSV, yes?
|
|
« Last Edit: April 26, 2007, 07:40:02 PM by Uisce Beatha »
|
Logged
|
"If you're darker than a caramel, Reverend Al speaks for you." - Aasif Mandvi "Well, you can tell by the way I use my walk, I'm a woman's man: no time to talk." - stroh
|
|
|
Aske
Lederhosen
Karma: 120 Posts: 31405
Offline
|
|
Re: c++ gurus? - read in arbitrary csv...
« Reply #2 on: April 26, 2007, 07:41:31 PM » |
|
It's been a million years and the last time I had C++ code laying around it was probably on a 286. But... strings are just arrays of characters. If you're comfortable with arrays just str.copy(buf, i) and go crazy. But you'll have to debug. Man, that was ages ago. I'm old. edit: If you're gonna break on whitespace it's technically not CSV, yes? , per your edit note... i was thinking about if the 'cell' is empty...
|
|
|
Logged
|
Russia has invaded a sovereign neighboring state and threatens a democratic government elected by its people. Such an action is unacceptable in the 21st century. -- Chimpy McFlightsuit, CEO of Bu$hco Industries of 'Merka
|
|
|
E-A-G-L-E!
Rich Corinthian Leather Jacket From: The Land of 10,000+ Slushy Ice Rinks
Karma: 23 Posts: 5095
Offline
|
|
Re: c++ gurus? - read in arbitrary csv...
« Reply #3 on: April 26, 2007, 07:45:09 PM » |
|
I was only *just* starting to learn c++ a year or so ago, then had to drop it 'cause of lack of time.
|
|
|
Logged
|
He is no fool who gives what he cannot keep to gain that which he cannot lose. - Jim Elliot
|
|
|
gleek
Flak Jacket
Karma: 107 Posts: 9511
OfflineE chu ta!
|
|
Re: c++ gurus? - read in arbitrary csv...
« Reply #4 on: April 26, 2007, 07:47:25 PM » |
|
http://www.linuxquestions...s/showthread.php?t=126337#include <iostream> #include <fstream> #include <strstream>
using namespace std;
int main() { char buffer1[2048]; char buffer2[2048]; istrstream ostr1(buffer1, 2048); istrstream ostr2(buffer2, 2048); int values1[100]; int values2[100]; int c=0;
ifstream fin("data.txt");
fin.getline(buffer1, 2048); fin.getline(buffer2, 2048);
while (ostr1 >> values1[c]) { ostr2 >> values2[c++]; }
for (int i=0;i<c;i++) { cout << values1[i] << ":" << values2[i] << endl; }
return 0; }
|
|
|
Logged
|
Woman, open the door, don't let it sting. I wanna breathe that fire again.
|
|
|
Aske
Lederhosen
Karma: 120 Posts: 31405
Offline
|
|
Re: c++ gurus? - read in arbitrary csv...
« Reply #5 on: April 26, 2007, 07:49:31 PM » |
|
http://www.linuxquestions...s/showthread.php?t=126337#include <iostream> #include <fstream> #include <strstream>
using namespace std;
int main() { char buffer1[2048]; char buffer2[2048]; istrstream ostr1(buffer1, 2048); istrstream ostr2(buffer2, 2048); int values1[100]; int values2[100]; int c=0;
ifstream fin("data.txt");
fin.getline(buffer1, 2048); fin.getline(buffer2, 2048);
while (ostr1 >> values1[c]) { ostr2 >> values2[c++]; }
for (int i=0;i<c;i++) { cout << values1[i] << ":" << values2[i] << endl; }
return 0; } thanks, I will give it a try.
|
|
|
Logged
|
Russia has invaded a sovereign neighboring state and threatens a democratic government elected by its people. Such an action is unacceptable in the 21st century. -- Chimpy McFlightsuit, CEO of Bu$hco Industries of 'Merka
|
|
|
gleek
Flak Jacket
Karma: 107 Posts: 9511
OfflineE chu ta!
|
|
Re: c++ gurus? - read in arbitrary csv...
« Reply #6 on: April 26, 2007, 07:51:18 PM » |
|
I don't want a large farva.
|
|
|
Logged
|
Woman, open the door, don't let it sting. I wanna breathe that fire again.
|
|
|
Aske
Lederhosen
Karma: 120 Posts: 31405
Offline
|
|
Re: c++ gurus? - read in arbitrary csv...
« Reply #7 on: April 26, 2007, 07:57:41 PM » |
|
I don't want a large farva.
Shenanigans.
|
|
|
Logged
|
Russia has invaded a sovereign neighboring state and threatens a democratic government elected by its people. Such an action is unacceptable in the 21st century. -- Chimpy McFlightsuit, CEO of Bu$hco Industries of 'Merka
|
|
|
Salamander
Vest & Plus Fours From: Blerghland
Karma: 18 Posts: 772
Offlinevärsta bergatroll
|
|
Re: c++ gurus? - read in arbitrary csv...
« Reply #8 on: April 27, 2007, 08:50:02 AM » |
|
I'll have to pistol whip you now.
|
|
|
Logged
|
Magnus Frater te spectat
|
|
|