Code Monkey home page Code Monkey logo

csvwriter's Introduction

CSVWriter

Simple c++ csv writer class.

How to use ?

just c&p or add the CSVWriter.h file to your project.

write a single row

CSVWriter csv;
csv << "this" << "is" << "a" << "row";
cout << csv << endl;

output:

this;is;a;row

write multiple rows

CSVWriter csv;
csv.newRow() << "this" << "is" << "the" << "first" << "row";
csv.newRow() << "this" << "is" << "the" << "second" << "row";
cout << csv << endl;

output:

this;is;the;first;row
this;is;the;second;row

auto row

When each row has a fixed number of colums, CSVWriter can add rows automatically. In this example, each row has 4 colums:

CSVWriter csv;
csv.enableAutoNewRow(5);
csv << "this" << "is" << "the" << "first" << "row" << "this" << "is" << "the" << "second" << "row";
cout << csv << endl;

output:

this;is;the;first;row
this;is;the;second;row

write csv to filesystem

CSVWriter csv;
csv.newRow() << "this" << "is" << "the" << "first" << "row";
csv.newRow() << "this" << "is" << "the" << "second" << "row";
csv.writeToFile("foobar.csv");

append csv to existing file

CSVWriter csv;
csv << "append" << "this" << "row" << "please" << ":)";
csv.writeToFile("foobar.csv",true);

change seperator

CSVWriter csv(",");
csv << "this" << "is" << "a" << "row";
cout << csv << endl;

output:

this,is,a,row

merge two CSVWriters

CSVWriter csv_a;
CSVWriter csv_b;
csv_a << "this" << "comes" << "from" << "csv_a";
csv_b << "this" << "is" << "from" << "csv_b";
csv_b += csv_a;
cout << csv_b << endl;

output:

this;is;from;csv_b
this;comes;from;csv_a

supported datatypes

CSVWriter uses a stringstream to serilize values. Each datatype supported by << operator from stringstresm can be used:

char c = 'c';
bool b = false;
short s = 6000;
int i = 300000;
float f = 3.14159;
double d = 4.85875e-270;
string str = "hello world";
char c_str[] = "whats up";
CSVWriter csv;
csv << c << b << s << i << f << d << str << c_str ;
cout << csv;

output:

c;0;6000;300000;3.14159;4.85875e-270;hello world;whats up

As you can see, boolean values are serilized to 0 for false and 1 for true.

escaping

CSVWriter can escape your seperator

CSVWriter csv;
csv << "escape;me;please";
cout << csv << endl;

output:

"escape;me;please"

CSVWriter can also escape quotation marks

CSVWriter csv;
csv << "escape\"me\"please\"\":)";
cout << csv << endl;

output:

"escape""me""please"""":)"

csvwriter's People

Contributors

al-eax avatar angelperezm 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.