The CSV file format, an acronym for "Comma-Separated Values", is commonly employed for storing and sharing tabular data.
- Information within CSV files is structured as plain text in a grid format of rows and columns.
- CSV files contain plain text information arranged in rows and columns.
Each row corresponds to a single record, with each column representing a specific field or attribute associated with that record. In CSV (Comma-Separated Values) files, data values are separated by commas, although semicolons and tabs can also be used as delimiters.
Example:
Input:
CSV File = "data.csv"
//The list includes: 1, Employee1, Haskell 2, Employee2, CSS 3, Employee3, Javascript
Output:
The elements within the 2D array are: { {1, Employee1, Haskell},
{2, Employee2, CSS},
{3, Employee3, Javascript} }
Characteristics of CSV files:
Several characteristics of CSV files in C++ are as follows:
- CSV files are simple and lightweight, which makes them easy to create, edit, and process.
- They are human-readable and can be opened and edited with text editors.
- CSV files are mostly used for storing raw data; they cannot support formatting or complicated data types like images or formulas.
Uses of CSV files:
Several uses of CSV files in C++ are as follows:
- CSV files are frequently used to store and exchange data across various software applications.
- Spreadsheet programs like Microsoft Excel, database management systems , and programming language s widely support them.
- CSV files are used for various tasks, including data backup, import/export, and sharing across systems.
- <iostream>: This module offers fundamental input/output stream functionalities that are necessary for handling console input/output.
- <fstream>: It allows the program to interact with external files by providing file input/output operations.
- <sstream>: It allows string data manipulation and parsing through string stream processing.
- <string>: It provides the string class and related methods for manipulating strings , which are essential for processing CSV data.
- std::istringstream Upon reading a line, a string stream (std::istringstream) is created from the line. It allows the line to be treated as a stream of characters.
- std::getline Tokens are extracted from the string stream by using another call to std::getline with a comma delimiter supplied. It divides the line into individual tokens, each of which represents a value separated by a comma.
Header Files Used:
Use of std::getline and std::istringstream to Read CSV File in C++:
What is a two-dimensional array or a 2D array?
A two-dimensional array, also known as a 2D array, functions as a data structure in C++ that exhibits a structural layout akin to a grid. In contrast to a one-dimensional array, which is a sequential arrangement of elements, a two-dimensional array comprises rows and columns that construct a grid or matrix.
Example:
const int rows = 4;
const int columns = 3;
int arr[rows][columns] =
{
{4, 7, 9},
{8, 10, 24},
{28, 17, 19},
{15, 22, 29}
};
Code Implementation:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
const int MAX_ROWS = 100;
// The 2D array's maximum number of rows.
const int MAX_COLUMNS = 100;
// The 2D array's maximum number of columns.
int main()
{
int data[MAX_ROWS][MAX_COLUMNS];
// 2D array for storing the data.
std::ifstream file("data.csv");
// Opening the CSV file.
if (!file.is_open())
{
std::cerr << "Error: Unable to open the file!" << std::endl;
return 1;
}
std::string line;
int row = 0;
while (std::getline(file, line))
{
std::istringstream iss(line);
std::string token;
int column = 0;
while (std::getline(iss, token, ','))
{
data[row][column] = std::stoi(token);
// Converting the string to integer.
column++;
}
row++;
}
file.close();
// Closing the CSV file.
// Printing the data.
for (int q = 0; q < row; q++)
{
for (int l = 0; l < MAX_COLUMNS; l++)
{
std::cout << data[q][l] << " ";
}
std::cout << std::endl;
}
return 0;
}
Output:
ERROR!
Error: Unable to open the file!
Explanation:
- This C++ code reads data into a 2D array from a CSV file.
- It starts by creating constants for the maximum number of rows and columns in the array and includes the required header files. Using an input file stream, the code opens the CSV file and verifies that it was opened successfully.
- After that, iterating over every line in the CSV file, it tokenizes each one using a comma as a delimiter and uses std::stoi function to transform each token from string to integer. The 2D array data[ contains the data, and row and column indices are used to track the data's position in the array.
- Once all of the data has been read from it, the file stream is closed. Ultimately, it prints the information kept in the 2D array row by row, putting a space between each element. This process is continued until all rows have been printed.
Complexity Analysis:
Time Complexity: O(R * C)
Where R represents the number of rows and C represents the number of columns.
Space Complexity: O(R * C)