Working with CSV in C++: Library Overview and Practical Examples
CSV (Comma-Separated Values) is one of the most popular formats for storing tabular data. C++ does not have built-in CSV support, but there are many libraries and approaches for reading, writing, and processing such files. In this article, we will look at the main ways to work with CSV in C++, including the popular libraries libcsv and rapidcsv, as well as implementing a parser using standard tools.
Why is CSV Important for C++?
CSV is used for data exchange between systems, exporting from databases, machine learning, and scientific calculations. In C++, working with CSV is often required in applications for processing logs, configurations, or importing data from Excel. Choosing the right library speeds up development and increases code reliability.
libcsv Library
libcsv is a lightweight C library that is well-suited for C++ projects. It supports CSV parsing with customizable delimiters, character escaping, and error handling.
- Support for various delimiters (comma, semicolon, tab).
- Handling of escaped strings and quotes.
- Minimal memory consumption.
Example of reading CSV using libcsv:
#include <csv.h>#include <iostream>#include <vector>#include <string>
struct csv_state { std::vector<std::vector<std::string>> rows;};
void cb1(void *s, size_t len, void *data) { auto *state = static_cast<csv_state*>(data); state->rows.back().push_back(std::string(static_cast<char*>(s), len));}
void cb2(int c, void *data) { auto *state = static_cast<csv_state*>(data); state->rows.emplace_back();}
int main() { struct csv_parser p; csv_init(&p, CSV_STRICT); csv_set_delim(&p, ',');
csv_state state; state.rows.emplace_back();
const char *input = "Name,Age,CityAlice,30,New YorkBob,25,London"; csv_parse(&p, input, strlen(input), cb1, cb2, &state); csv_fini(&p, cb1, cb2, &state);
for (auto &row : state.rows) { for (auto &col : row) { std::cout << col << " "; } std::cout << std::endl; }
csv_free(&p); return 0;}rapidcsv Library
rapidcsv is a modern C++ library with a convenient interface. It supports reading and writing CSV, automatic data type detection, and working with headers.
- Simple API based on STL containers.
- Automatic conversion of strings to numbers.
- Support for custom delimiters and encodings.
Example of writing and reading CSV with rapidcsv:
#include <rapidcsv.h>#include <iostream>
int main() { // Writing CSV rapidcsv::Document doc("", rapidcsv::LabelParams(0, 0)); doc.SetCell<std::string>(0, 0, "Product"); doc.SetCell<int>(1, 0, 2023); doc.SetCell<double>(2, 0, 99.99); doc.Save("data.csv");
// Reading CSV rapidcsv::Document docRead("data.csv"); std::string product = docRead.GetCell<std::string>(0, 0); int year = docRead.GetCell<int>(1, 0); double price = docRead.GetCell<double>(2, 0);
std::cout << "Product: " << product << ", Year: " << year << ", Price: " << price << std::endl; return 0;}Working with CSV Using Standard C++ Tools
If you do not want to include external libraries, you can implement a parser using std::stringstream and std::getline. This is suitable for simple CSV without escaping.
#include <iostream>#include <fstream>#include <sstrea Also in library
Swagger/OpenAPI Generation for TypeScript: Libraries and Tools
HTTP client for TypeScript: top libraries for modern web applications
CLI framework for Rust: Clap — a powerful and convenient tool for creating console applications
OpenSSL C++ Guide: Complete Tutorial with Code Examples