Giter VIP home page Giter VIP logo

csv-cpp's Introduction

CSV-cpp

CSV-cpp is a simple and easy to use, header-only comma separated values (CSV) en- and decoder for C++.

Install

Install the headers using the CMake build system:

cd <path-to-repo>
git submodule update --init --recursive
mkdir build
cd build
cmake ..
make install

or simply copy the header file into your project and include it directly.

Usage

CSV-cpp allows loading data from any std::istream. Simply call the decode() method or use the overloaded constructor.

#include <csvcpp.h>

int main()
{
	// create istream object "is" ...

	// use function
	csv::CsvFile myCsv;
	myCsv.decode(is);

	// or use constructor
	csv::CsvFile myCsv(is);
}

For convenience there is also a load() method that expects a file name and allows to parse file contents directly.

Rows and columns parsed from the stream can be accessed using the index operator []. The extracted value can be converted to various native types.

bool myBool = myCsv[0][0].as<bool>();
std::string myStr = myCsv[0][1].as<std::string>();
const char *myCStr = myCsv[0][2].as<const char *>();
int myInt = myCsv[0][3].as<int>();
unsigned int myUInt = myCsv[0][4].as<unsigned int>();
double myDouble = myCsv[0][5].as<double>();
float myFloat = myCsv[0][6].as<float>();

Natively supported types are:

  • const char *
  • std::string
  • int
  • unsigned int
  • bool
  • float
  • double

Custom type conversions can be added by implementing an explicit cast and assignment operator for CsvValue.

#include <csvcpp.h>

struct MyDataType
{
	// ...
};

explicit operator MyDataType(const csv::CsvValue &csvVal)
{
	// convert std::string to your data type
	// return MyDataType::fromStr(csvVal.value_);
}

csv::CsvValue &operator=(csv::CsvValue &csvVal, const MyDataType &value)
{
	// conert your data type to a string
	// csvVal.value_ = value.toStr();
	// return csvVal;
}

Values can be assigned to csv values just by using the assignment operator. The content of the csv file can then be written to any std::ostream object.

#include <csvcpp.h>

int main()
{
	// create ostream object "os" ...

	csv::CsvFile myCsv;

	myCsv.push_back({1, "Hello world", true, 1.2});
	myCsv.push_back({"Foobar", 2.1f, -2});

	myCsv.encode(os);
}

For convenience there is also a save() function that expects a file name and stores the values of the CsvFile object in that file.

Custom value separator and comment character can be set with their setter functions or with one of the overloaded constructors.

#include <csvcpp.h>

int main()
{
	char sep = '\t';
	char comment = ';';

	// use the constructor
	csv::CsvFile myCsv1(sep, comment);

	// use the setter
	csv::CsvFile myCsv2;
	myCsv2.setValueSeparator(sep);
	myCsv2.setCommentChar(comment);
}

If your CSV rows are very long you can set a proposed row length (number of csv values per row) to make parsing more efficient.

#include <csvcpp.h>

int main()
{
	size_t rowLen = 2056;

	// use the setter
	csv::CsvFile myCsv;
	myCsv.proposeRowLength(rowLen);
}

csv-cpp's People

Contributors

rookfighter avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.