C++:
Working with CSV

How to:

Reading a CSV file using C++ Standard Library:

#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>

int main() {
    std::ifstream file("data.csv");
    std::string line;
    
    while (std::getline(file, line)) {
        std::stringstream lineStream(line);
        std::string cell;
        std::vector<std::string> parsedRow;
        
        while (std::getline(lineStream, cell, ',')) {
            parsedRow.push_back(cell);
        }
        
        // Process parsedRow here
        for (const auto& val : parsedRow) {
            std::cout << val << "\t";
        }
        std::cout << std::endl;
    }
    
    return 0;
}

Writing to a CSV file:

#include <fstream>
#include <vector>

int main() {
    std::ofstream file("output.csv");
    std::vector<std::vector<std::string>> data = {
        {"Name", "Age", "City"},
        {"John Doe", "29", "New York"},
        {"Jane Smith", "34", "Los Angeles"}
    };
    
    for (const auto& row : data) {
        for (size_t i = 0; i < row.size(); i++) {
            file << row[i];
            if (i < row.size() - 1) file << ",";
        }
        file << "\n";
    }
    
    return 0;
}

Using a third-party library: csv2:

While the C++ Standard Library provides the basic tools for working with files and strings, leveraging third-party libraries can simplify CSV processing. One such library is csv2, known for its ease of use and efficiency.

  • Installation: Typically installed via package managers like Conan or directly from its GitHub repository.

Example using csv2 to read a CSV file:

#include <csv2/reader.hpp>
#include <iostream>

int main() {
    csv2::Reader<csv2::delimiter<','>, csv2::quote_character<'"'>, csv2::first_row_is_header<true>> csv;
    if (csv.mmap("data.csv")) {
        const auto header = csv.header();
        for (const auto row : csv) {
            for (const auto cell : row) {
                std::cout << cell.second << "\t"; // Print each cell value
            }
            std::cout << std::endl;
        }
    }
    return 0;
}

Sample output for reading operations might look like this (assuming a simple three-column CSV file):

John    29    New York    
Jane    34    Los Angeles

These examples aim to cover fundamental CSV operations in C++. For more complex scenarios, like dealing with large files or complex data transformations, further exploration into specialized libraries or tools might be warranted.