How to Read Data from a CSV File to a 2D Array in C++?
Last Updated :
02 Apr, 2024
A comma-separated value file also known as a CSV file is a file format generally used to store tabular data in plain text format separated by commas. In this article, we will learn how we can read data from a CSV file and store it in a 2D array in C++.
Example:
Input:
CSV File = “data.csv”
//contains: 1, Student1, C++ 2, Student2, Java 3, Student3, Python
Output:
2D array elements: { {1, Student1, C++},
{2, Student2, Java},
{3, Student3, Python} }
Read CSV Data into a 2D Array in C++
To read data from a CSV file into a 2D array in C++, we can use the file streams from the standard library, std::ifstream
for reading from files to read the file line by line, and for each line, we use a std::stringstream
to split the line into individual values using the comma as a delimiter.
Note: All this data will be stored as a single data types as array cannot contain different data types. One way is to store all the data in the string and then convert it later accordingly.
C++ Program to Read data from a CSV file into a 2D Array
The following program illustrates how we can read data from a CSV file into a 2D array in C++.
C++
// C++ Program to illustrate how we can read data from a
// CSV file into a 2D array
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;
// Maximum number of rows for the 2D array
const int MAX_ROWS = 100;
// Maximum number of columns for the 2D array
const int MAX_COLS = 100;
int main()
{
// Open the CSV file
ifstream file("data.csv");
if (!file.is_open()) {
cerr << "Error opening file!" << endl;
return 1;
}
// Define a 2D array to store the CSV data
string data[MAX_ROWS][MAX_COLS];
string line;
int row = 0;
// Store the CSV data from the CSV file to the 2D array
while (getline(file, line) && row < MAX_ROWS) {
stringstream ss(line);
string cell;
int col = 0;
while (getline(ss, cell, ',') && col < MAX_COLS) {
data[row][col] = cell;
col++;
}
row++;
}
// close the file after read opeartion is complete
file.close();
// Print the data stored in the 2D array
for (int i = 0; i < row; i++) {
for (int j = 0; j < MAX_COLS && !data[i][j].empty();
j++) {
cout << data[i][j] << " ";
}
cout << endl;
}
return 0;
}
Output
1 Student1 C++ 2 Student2 Java 3 Student3 Python
Time Complexity: O(R * C), here R is the number of rows and C is the number of columns.
Auxiliary Space: O(R * C)
Similar Reads
How to Read and Write Arrays to/from Files in C++? In C++, an array is a collection of elements of the same type placed in contiguous memory locations. A file is a container in a computer system for storing information. In this article, we will learn how to read and write arrays to/from files in C++. Example: Input: Array = {1, 2, 3, 4, 5}Output: //
2 min read
How to Pass a 3D Array to a Function in C++? In C++, a 3D array is a multidimensional array that has three dimensions, i.e. it can grow in three directions. In this article, we will learn how to pass a 3D array to a function in C++. Pass a 3D Array to a Function in C++ Just like normal 1-dimensional arrays, we can't pass the array to a functio
2 min read
How to Read From a File in C++? Reading from a file means retrieving the data stored inside a file. C++ file handling allows us to read different files from our C programs. This data can be taken as input and stored in the program for processing. Generally, files can be classified in two types:Text File: Files that contains data i
4 min read
How to Read Binary Search Tree from File in C++? A binary search tree is a hierarchical data structure in which for every node in the tree, the value of all nodes in the left subtree is less than the node's value and the value of all nodes in the right subtree is greater than the node's value. This property of the binary search tree makes it effic
4 min read
How to Create a Map of Arrays in C++? In C++, the std::map is a container that stores elements in a key-value pair, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a map of arrays in C++. Example: Input: arr1 = {1, 2, 3};arr2 = {4, 5, 6};arr3 = {7, 8, 9};
2 min read