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

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 filestd::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 cols.push_back(col); } // 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 filecsvFile << "Column1,Column2,Column3\n"; // write header rowcsvFile << "1,Hello,World\n"; // write data rowscsvFile << "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 modecsvFile << "3,Greetings,Earth\n"; // append a new rowIn 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.
© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js