Sai A Sai A
Updated date Dec 20, 2023
In this blog, we will discover easy ways to convert CSV data to strings in C++. Learn from simple code examples using the C++ Standard Library, Boost libraries, and RapidCSV.

Introduction:

Working with CSV files in C++ might sound complex, but it's a common task that can be simplified. In this blog, we will explore simple ways to convert CSV data into a string using C++. 

Method 1: Using C++ Standard Library

Let's start with a easiest approach using the tools provided by the C++ Standard Library.

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

std::string csvToString(const std::string& filename) {
    std::ifstream file(filename);
    if (!file.is_open()) {
        std::cerr << "Error: Couldn't open file " << filename << std::endl;
        return "";
    }

    std::ostringstream oss;
    oss << file.rdbuf();
    file.close();

    return oss.str();
}

int main() {
    std::string csvContent = csvToString("sample.csv");
    std::cout << "Method 1 Output:\n" << csvContent << std::endl;

    return 0;
}

This method opens the CSV file, reads its content, and stores it in a string. The resulting string is then printed.

Output:

Method 1 Output:
Name, Age, City
John, 25, New York
Jane, 30, Los Angeles
Bob, 22, Chicago

Method 2: Using Boost C++ Libraries

Boost libraries provide additional functionalities for C++. Here's a simple method using Boost.IOStreams.

#include <iostream>
#include <fstream>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/copy.hpp>

std::string csvToStringBoost(const std::string& filename) {
    std::ifstream file(filename, std::ios_base::binary);
    if (!file.is_open()) {
        std::cerr << "Error: Couldn't open file " << filename << std::endl;
        return "";
    }

    boost::iostreams::stream<boost::iostreams::file_source> input(file);
    std::ostringstream oss;
    boost::iostreams::copy(input, oss);

    return oss.str();
}

int main() {
    std::string csvContentBoost = csvToStringBoost("sample.csv");
    std::cout << "Method 2 Output:\n" << csvContentBoost << std::endl;

    return 0;
}

This method uses Boost.IOStreams to read the CSV file and copy its content to a string.

Output:

Method 2 Output:
Name, Age, City
John, 25, New York
Jane, 30, Los Angeles
Bob, 22, Chicago

Method 3: Using RapidCSV Library

RapidCSV is a lightweight library designed for easy CSV handling. Here's how you can use it.

#include <iostream>
#include "rapidcsv.h"

std::string csvToStringRapidCSV(const std::string& filename) {
    rapidcsv::Document doc(filename);
    std::ostringstream oss;
    doc.Save(oss);

    return oss.str();
}

int main() {
    std::string csvContentRapidCSV = csvToStringRapidCSV("sample.csv");
    std::cout << "Method 3 Output:\n" << csvContentRapidCSV << std::endl;

    return 0;
}

This method uses the RapidCSV library to read the CSV file and save its content to a string.

Output:

Method 3 Output:
Name,Age,City
John,25,New York
Jane,30,Los Angeles
Bob,22,Chicago

Conclusion:

In this blog, we have covered three simple ways to convert CSV data into a string in C++. You can choose the method that fits your project best, whether it's using the C++ Standard Library, Boost for additional features, or RapidCSV for lightweight handling. 

Comments (0)

There are no comments. Be the first to comment!!!