GolfHos

General => The Cantina => Topic started by: Aske on April 26, 2007, 07:23:37 PM



Title: c++ gurus? - read in arbitrary csv...
Post by: Aske 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...


Title: Re: c++ gurus? - read in arbitrary csv...
Post by: Uisce Beatha 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.   [sm_devil]

Man, that was ages ago.  I'm old.

edit:  If you're gonna break on whitespace it's technically not CSV, yes?   [sm_devil]


Title: Re: c++ gurus? - read in arbitrary csv...
Post by: Aske 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.   [sm_devil]

Man, that was ages ago.  I'm old.

edit:  If you're gonna break on whitespace it's technically not CSV, yes?   [sm_devil]


per your edit note... i was thinking about if the 'cell' is empty...   


Title: Re: c++ gurus? - read in arbitrary csv...
Post by: E-A-G-L-E! 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.  :sad3:


Title: Re: c++ gurus? - read in arbitrary csv...
Post by: gleek on April 26, 2007, 07:47:25 PM
http://www.linuxquestions.org/questions/showthread.php?t=126337

Code:
#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;
}


Title: Re: c++ gurus? - read in arbitrary csv...
Post by: Aske on April 26, 2007, 07:49:31 PM
http://www.linuxquestions.org/questions/showthread.php?t=126337

Code:
#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.
 [sm_beertoast]


Title: Re: c++ gurus? - read in arbitrary csv...
Post by: gleek on April 26, 2007, 07:51:18 PM
I don't want a large farva.


Title: Re: c++ gurus? - read in arbitrary csv...
Post by: Aske on April 26, 2007, 07:57:41 PM
I don't want a large farva.

Shenanigans.


Title: Re: c++ gurus? - read in arbitrary csv...
Post by: Salamander on April 27, 2007, 08:50:02 AM
I'll have to pistol whip you now.