David's Blog

C++ I/O: Reading and Manipulating CSV Data .

By David Li on Saturday, 26 December 2024 13:00:00 GMT

C++ I/O: Reading and Manipulating CSV Data

CSV or Comma-Separated Values is a widely used format for storing tabular data. In this article, we will delve into how to read and manipulate CSV data in a C++ program using simple I/O operations.

Reading CSV Data To read a CSV file, we can use std::ifstream to open the file and read rows one by one. A single row can be read into a string using std::getline(). We can then parse the string to extract individual columns. Here is an example:

std::ifstream csvFile {"data.csv"}; // open the file
std::string row;
while (std::getline(csvFile, row)) { // read rows one by one
    std::stringstream rowStream(row); // create a stringstream from the row string
    std::string col;
    std::vector<std::string> cols; // a vector to store the columns
    while (std::getline(rowStream, col, ',')) { // parse the row string and extract columns
    // Access columns using cols 

In the above example, we opened the CSV file “data.csv” using std::ifstream. We then read each row of the file using std::getline() and stored it in the string variable “row”. We created a stringstream from the “row” string and extracted individual columns using std::getline() with delimiter ’,’ (comma) in a while loop. The extracted columns were then stored in a vector called “cols” for further processing.

Manipulating CSV Data We can also write CSV data from our program using std::ofstream. We can open a file for writing and write rows and columns separated by commas. Here is an example:

std::ofstream csvFile {"data.csv"}; // open the file
csvFile << "Column1,Column2,Column3\n"; // write header row
csvFile << "1,Hello,World\n"; // write data rows
csvFile << "2,Goodbye,Moon\n";

In the above example, we opened the CSV file “data.csv” using std::ofstream. We then wrote a header row and two data rows separated by commas. If we want to append to an existing CSV file, we can use std::ofstream with the std::ios::app flag like this:

std::ofstream csvFile {"data.csv", std::ios::app}; // open the file in append mode
csvFile << "3,Greetings,Earth\n"; // append a new row

In conclusion, CSV files can be easily read and written in C++ using basic I/O operations and string parsing techniques. CSV is a simple yet powerful format for data exchange between programs. With the above examples, you should be able to read and manipulate CSV data in your C++ programs.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-05-06.