0

hi

i am a beginner with c++. i have a csv file. i want to read particular set of values from a particular column. how can i do it. if possible kindly help me with some sample codes.
thanks in advance

Edited by Nantha_1

4
Contributors
7
Replies
36
Views
3 Months
Discussion Span
Last Post by DeanMSands3
1

This can be tricky depending on the cleanliness of the CSV.
Was it created manually, or with Excel?
Will there be commas in quoted fields?
What compiler are you using?
What techniques are you not allowed to use?

0

The datas are created by software. the values are like, for example like 3,32 ; 0 ; 22,00 ; 0,2.
it has multiple rows and columns.
i want to read particular set of values from a particular column depending on predefined condition( < or > ).
regarding the techniques, im allowed to use anything.

1

My technique uses C++/CLI, the System.Core reference and the System, System::Collections::Generic, System::IO and System::Linq namespaces.
I would use the File::ReadAllLines() method to read the entire file at once.

I would then use the Enumerable::Select() method to take each row and pass it off to a method that Splits the row by commas and convert the entire collection into a List of array of strings List<array<String^>^>^ or an array of array of strings array<array<String^>^>^

At that point, I could have access to any of the elements like a two-dimensional array.

Edited by thines01: Formatting

0

A better technique may be to use the OleDb namespace and methods.
The code may be more verbose, but it is a solid technique.

0

Week-old post is a week-old. Never-the-less!
You can parse CSV's in 3 different ways:

  1. Use a Finite State Machine to parse through the lines. https://stackoverflow.com/a/30338543

  2. Use a RegEx to parse through the lines. (Technically, still an FSM, but smaller.)
    https://stackoverflow.com/questions/18144431/regex-to-split-a-csv

  3. Use a library someone else already wrote. https://github.com/ben-strasser/fast-cpp-csv-parser

Also, a good reference:
https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.