#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
int main() {
// Step 1: Open the CSV file
std::ifstream file("example.csv");
// Step 2: Check if the file is open
if (!file.is_open()) {
std::cerr << "Error opening file" << std::endl;
return 1;
}
// Step 3: Define variables to store CSV data
std::vector<std::vector<std::string>> data;
// Step 4: Read the CSV file line by line
std::string line;
while (std::getline(file, line)) {
// Step 5: Create a stringstream to parse each line
std::istringstream iss(line);
std::vector<std::string> row;
// Step 6: Read each element separated by commas
std::string element;
while (std::getline(iss, element, ',')) {
// Step 7: Add each element to the row vector
row.push_back(element);
}
// Step 8: Add the row vector to the data vector
data.push_back(row);
}
// Step 9: Close the CSV file
file.close();
// Step 10: Display the read data
for (const auto& row : data) {
for (const auto& element : row) {
std::cout << element << " ";
}
std::cout << std::endl;
}
return 0;
}