#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
int main() {
std::ifstream file("data.txt"); // Open the file named "data.txt"
if (!file.is_open()) { // Check if the file opened successfully
std::cerr << "Error opening file." << std::endl;
return 1; // Exit the program with an error code
}
std::vector<std::vector<std::string>> data; // Create a 2D vector to store the data
std::string line; // Variable to store each line of the file
while (std::getline(file, line)) { // Read each line of the file
std::vector<std::string> row; // Create a vector to store values from each line
std::stringstream ss(line); // Create a stringstream to parse the line
std::string value; // Variable to store each value separated by commas
while (std::getline(ss, value, ',')) { // Read each value separated by commas
row.push_back(value); // Add the value to the row vector
}
data.push_back(row); // Add the row vector to the 2D data vector
}
file.close(); // Close the file
// Displaying the read data
for (const auto& row : data) { // Loop through the rows
for (const auto& value : row) { // Loop through the values in each row
std::cout << value << " "; // Output the value
}
std::cout << std::endl; // Move to the next line after each row
}
return 0; // Return from the program
}