The conversion can be tricky (does your csv allow quoted commas?) but you could just translate all ',' to ' ' and parse using a stringstream. For example:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
int main () {
std::string s = "123.45,213.76,234.16";
std::size_t pos = 0;
std::vector< double > vd;
double d = 0.0;
// convert ',' to ' '
while (pos < s.size ())
if ((pos = s.find_first_of (',',pos)) != std::string::npos)
s[pos] = ' ';
std::stringstream ss(s);
while (ss >> d)
vd.push_back (d);
for (int i = 0; i < vd.size (); ++i)
std::cout << "vd[" << i << "] = " << vd[i] << std::endl;
return 0;
}
L7Sqr
Practically a Master Poster
656 posts since Feb 2011
Reputation Points: 201
Solved Threads: 123
You need to parse the line. Find each (,) and
1) Convert the value to float and store it.
2) Search for a comma
3) If found, skip over it and go back to 1
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Thank You, this code works for me but when I try and sub the lastLine I get from my code in place of your s it does not work? Why is this?
Probably because you tried to add code you don't understand and expect it to work without modification. It's obvious from the code you postedL7Sqr's code is far beyond your level.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
If each entry in the file is comma separated(every field separated by a comma, no new line char or other whitespace), then you can read the file using getline() with the comma char as the delimiting char to "find" each comma. After each read from the file into a string using getline() you can convert that string into type double using a stringstream (although there are other, less "suitable" ways to convert from string to numerical types, too) and place the numerical value into an array of type double if that's what you want to do.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
It is obvious to me that common decency is far beyond your level.
Common decency? I gave you an answer that will work extremely well. You ignored it.
Can you explain the code fromL7Sqr? If so, you should know why didn't it work? If not, that's why it didn't work. Has nothing to do with common decency.
Look at your post:
Thank You, this code works for me but when I try and sub the lastLine I get from my code in place of your s it does not work? Why is this?
Common decency dictates you explain what happened, not force us to use psychic powers to figure out what you did wrong. Show us.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
My post was meant to illustrate a point, not provide an exact solution. Forgive me if I did a poor job explaining what is happening there.
In essence, I've done the same thing a WaltP suggests except that I add the step of converting commas to spaces to allow standard facilities to do the 'heavy lifting' for me.
To reiterate the point in posts above, what exactly did not work for you? What output did you expect - what did you get?
L7Sqr
Practically a Master Poster
656 posts since Feb 2011
Reputation Points: 201
Solved Threads: 123