Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a csv file data into an array?

Tags:

c++

I have a CSV file formatted as below:

1,50,a,46,50,b
2, 20,s,56,30,f
3,35,b,5,67,s
...

How can I turn that to a 2D array so that I could do some calculations?

void Core::parseCSV(){
    std::ifstream  data("test.csv");
    std::string line;
    while(std::getline(data,line))
    {
        std::stringstream lineStream(line);
        std::string cell;
        while(std::getline(lineStream,cell,','))
        {
            //not sure how to create the 2d array here
        }
    }
};
like image 344
hmaxx Avatar asked Dec 11 '15 07:12

hmaxx


People also ask

How to read a CSV file into an array in Python?

Use numpy.loadtxt () to Read a CSV File Into an Array in Python As the name suggests, the open () function is used to open the CSV file. Numpy’s loadtxt () function helps in loading the data from a text file.

What are data arrays in CSV files?

These types of files are used to store data in the form of tables and records. In these tables, there are a lot of columns separated by commas. One of the tasks in manipulating these CSV files is importing these files in the form of data arrays.

What is the use of CSV file?

CSV stands for Comma Separated Values. These types of files are used to store data in the form of tables and records. In these tables, there are a lot of columns separated by commas. One of the tasks in manipulating these CSV files is importing these files in the form of data arrays.

How do I read a CSV file in PHP?

In PHP, there is a function called fgetcsv, which will automatically parse the CSV fields of a given resource descriptor. Here is a simple function that shows how to read our CSV file and returns a multidimensional array containing the CSV data.


2 Answers

Try this,

void Core::parseCSV()
{
    std::ifstream  data("test.csv");
    std::string line;
    std::vector<std::vector<std::string> > parsedCsv;
    while(std::getline(data,line))
    {
        std::stringstream lineStream(line);
        std::string cell;
        std::vector<std::string> parsedRow;
        while(std::getline(lineStream,cell,','))
        {
            parsedRow.push_back(cell);
        }

        parsedCsv.push_back(parsedRow);
    }
};
like image 156
Jain Avatar answered Nov 13 '22 10:11

Jain


Following code is working. hope it can help you. It has a CSV file istream_iterator-like class. It is a template so that it can read strings, ints, doubles, etc. Its constructors accept a char delimiter, so that it may be used for more than strictly comma-delimited files. It also has a specialization for strings so that white space characters could be retained.

#include <iostream>
#include <sstream>
#include <fstream>
#include <iterator>

using namespace std;

template <class T>
class csv_istream_iterator: public iterator<input_iterator_tag, T>
{
    istream * _input;
    char _delim;
    string _value;
public:
    csv_istream_iterator( char delim = ',' ): _input( 0 ), _delim( delim ) {}
    csv_istream_iterator( istream & in, char delim = ',' ): _input( &in ), _delim( delim ) { ++*this; }

    const T operator *() const {
        istringstream ss( _value ); 
        T value;
        ss >> value;
        return value;
    }

    istream & operator ++() {
        if( !( getline( *_input, _value, _delim ) ) )
        {
            _input = 0;
        }
        return *_input;
    }

    bool operator !=( const csv_istream_iterator & rhs ) const {
        return _input != rhs._input;
    }
};

    template <>
    const string csv_istream_iterator<string>::operator *() const {
        return _value;
    }

    int main( int argc, char * args[] )
    {
        { // test for integers
            ifstream fin( "data.csv" );
            if( fin )
            {
                copy( csv_istream_iterator<int>( fin ),
                      csv_istream_iterator<int>(),
                      ostream_iterator<int>( cout, " " ) );

                fin.close();
            }
        }

        cout << endl << "----" << endl;

        { // test for strings
            ifstream fin( "data.csv" );
            if( fin )
            {
                copy( csv_istream_iterator<string>( fin ),
                      csv_istream_iterator<string>(),
                      ostream_iterator<string>( cout, "|" ) );

                fin.close();
            }
        }

        return 0;
    }
like image 21
Confiz-3C Avatar answered Nov 13 '22 11:11

Confiz-3C