-4

I've attempted to use atof() (which I think is way off) and stringstream. I feel like stringstream is the answer, but I'm so unfamiliar with it. Based on some Google searches, YouTube videos, and some time at cplusplus.com, my syntax looks like below. I'm pulling data from a .csv file and attempting to put it into a std::vector<double>:

while (file.good() )
    {
    getline(file,line,',');
    stringstream convert (line);
    convert = myvector[i];
    i++;
    }
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Chase
  • 1
  • 1
  • 3
    [Browsing my trusty C++ documentation](https://en.cppreference.com/w/cpp/string/basic_string/stof), `std::stod` sounds like a good place to start. – user4581301 Sep 25 '18 at 22:33
  • 1
    Simply googling the title of your question would give you many answers. – interjay Sep 25 '18 at 22:35
  • 1
    I googled "c++ convert string to double" and the first two matches were std::stod and atof. http://www.cplusplus.com/reference/string/stod/ http://www.cplusplus.com/reference/cstdlib/atof/ Both look good. – Kenny Ostrom Sep 25 '18 at 22:35
  • 2
    `std::stod()` is best, but even in the code provided, you could just replace `convert = myvector[i];` with `convert >> myvector[i];` instead - assuming the `vector` has been preallocated beforehand, otherwise you would need to use `double value; convert >> value; myvector.push_back(value);` instead . Also, you should use `std::istringstream` instead of `std::stringstream`. – Remy Lebeau Sep 25 '18 at 22:35
  • Also see https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong –  Sep 25 '18 at 22:36

1 Answers1

1

If you are reading doubles from a stream (file) we can simplify this:

double  value;
while(file >> value) {
    myvector.push_back(value);
}

The operator>> will read from a stream into the type you want and do the conversion automatically (if the conversions exists).

You could use a stringstream as an intermediate if each line had more information on it. Like a word an integer and a double.

std::string line;
while(std::getline(file, line)) {
    std::stringstream lineStream(line);

    std::string word;
    int         integer;
    double      real;

    lineStream >> word >> integer >> real;
}

But this is overkill if you just have a single number on each line.

Now lets look at a csv file.
This is a line based file but each value is seporated by ,. So here you would read a line then loop over that line and read the value followed by the comma.

std::string line;
while(std::getline(file, line)) {
    std::stringstream lineStream(line);

    double value;
    char   comma;
    while(lineStream >> value) {
        // You successfully read a value
        if (!(lineStream >> comma && comma == ',')) {
            break; //  No comma so this line is finished break out of the loop.
        }
    }
}

Don't put a test for good() in the while condition.
Why is iostream::eof inside a loop condition considered wrong?

Also worth a read: How can I read and parse CSV files in C++?

Martin York
  • 257,169
  • 86
  • 333
  • 562