Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read and manipulate CSV file data in C++? [duplicate]

Tags:

c++

csv

Pretty self-explanatory, I tried google and got a lot of the dreaded expertsexchange, I searched here as well to no avail. An online tutorial or example would be best. Thanks guys.

like image 431
zkwentz Avatar asked Jan 06 '09 04:01

zkwentz


People also ask

How do you read and write in CSV at the same time?

You can do open("data. csv", "rw") , this allows you to read and write at the same time.


2 Answers

More information would be useful.

But the simplest form:

#include <iostream> #include <sstream> #include <fstream> #include <string>  int main() {     std::ifstream  data("plop.csv");      std::string line;     while(std::getline(data,line))     {         std::stringstream  lineStream(line);         std::string        cell;         while(std::getline(lineStream,cell,','))         {             // You have a cell!!!!         }     }  } 

Also see this question: CSV parser in C++

like image 161
Martin York Avatar answered Oct 06 '22 01:10

Martin York


You can try the Boost Tokenizer library, in particular the Escaped List Separator

like image 27
Alessandro Jacopson Avatar answered Oct 06 '22 01:10

Alessandro Jacopson