Re: CSV reading (help pls)
[bump] Could really do with any help on this [/bump]
Re: CSV reading (help pls)
use a std::vector instead ... see below for sample:
Code:
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
int main()
{
// std::ifstream ifs(filename);
std::string line, field;
std::vector<double> pArray;
double value;
// std::getline(ifs,line);
line = "1.2 , 2.033, 3.03 , 4.03";
std::stringstream ss(line);
while (ss >> value)
{
pArray.push_back(value);
std::getline(ss,field,',');
}
for (int i=0; i<pArray.size(); ++i)
{
std::cout << pArray[i] << "\n";
}
return 0;
}
Re: CSV reading (help pls)
Quote:
Originally Posted by sheepdonkey
0.4 in a CSV would be 0.4000000002
floating point values are not infinitely accurate.
They can't represent exactly decimal numbers.
There is not much you can do (except using greater floating point types):
Quote:
Originally Posted by sheepdonkey
double* pArray = new double[11];
Why do you dynamically allocate that.
A simple C-style array, or a std::vector (it it may be dynamic) would do the work, without memory leak.
Quote:
Originally Posted by sheepdonkey
std::getline(ss,field,',');
You'd rather use istream::ignore.
Re: CSV reading (help pls)
Thanks for the suggestions, they are just what I was looking for.