0

I have a .CSV file that's storing data from a laser. It records the height of the laser beam every second.

The .CSV file ends up having rows for each measurement that are all in this format:

DR,04,#

where the # is the height reading.

For example, if the beam is at a height of 10, the reading would say:

DR,04,10.

I want my program in C++ to read only the height (third column of the .CSV) from each row and put it into an array. I do not want the first two columns at all. That way I end up with an array with just a bunch of height values from each measurement.

How do I do that?

Athena
  • 11
  • 1
  • 1
    Don't you have any concrete attempts you're stuck with? If so elaborate where specifically. Otherwise your question looks just off-topic. – πάντα ῥεῖ Jan 13 '16 at 18:59
  • @πάντα ῥεῖ , I am a beginner at programming and have never programmed in C++ before. That's why I do not have any concrete attempts, and I figured someone would be able to point me in the right direction. I don't understand how my question is off-topic. – Athena Jan 13 '16 at 20:40
  • This might be helpful: http://stackoverflow.com/q/1120140/14065 – Martin York Jan 13 '16 at 22:59

1 Answers1

1

You can use strtok() to separate out the three columns. And then just get the last value.

You could also just take the string and scan for the first comma, and then scan from there for the second comma. What follows is the value you are after.

You could also use sscanf() to parse out the individual values.

This really isn't a difficult problem, and there are many ways to approach it. That is why people are complaining that you probably should've tried something and then ask a question here when you get stuck on a specific question.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • In this case it would work, but it's better to count the double quotes : "" in order to figure if the ',' was in the column or was a column separator. To make a generic function for any csv. – Pierre Emmanuel Lallemant Jan 13 '16 at 19:16
  • @PierreEmmanuelLallemant: A generic CSV parser (which I've written many of) was not called for here. All indications are that the field values will never contain a comma or any other character that requires quotes. – Jonathan Wood Jan 13 '16 at 20:11
  • @JonathanWood `strtok()` is one of the worst recommendations you can give. – πάντα ῥεῖ Jan 13 '16 at 20:48
  • @PierreEmmanuelLallemant: Yes, but I disagree with you that it is better to handle double quotes in this case. – Jonathan Wood Jan 13 '16 at 20:50
  • @πάνταῥεῖ: Thanks for making an assertion without justification. I'm sure everyone will find that very helpful. – Jonathan Wood Jan 13 '16 at 20:51