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?

3
  • 1
    Don't you have any concrete attempts you're stuck with? If so elaborate where specifically. Otherwise your question looks just off-topic. Commented Jan 13, 2016 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. Commented Jan 13, 2016 at 20:40
  • This might be helpful: stackoverflow.com/q/1120140/14065 Commented Jan 13, 2016 at 22:59

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
@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.
@JonathanWood strtok() is one of the worst recommendations you can give.
@PierreEmmanuelLallemant: Yes, but I disagree with you that it is better to handle double quotes in this case.
@πάνταῥεῖ: Thanks for making an assertion without justification. I'm sure everyone will find that very helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.