CSV is the short form of comma-separated values, which are used to store data that is represented in tabular form with the extension “.csv”. In this, each comma-separated value represents a data field or a record where each line denotes a single record against an object. C++ does not support an built-in library to read CSV files, but we can read them as other text files are being read. CSV files are read line by line and each value of one record is separated by the delimiter comma “,” while each row is separated by the new line delimiter “/n”. CSV files are mostly used to transfer data from excel to MySQL. It allows companies to transfer a high volume of data to a more compressed database.
Syntax
There is no predefined syntax for reading a CSV file in C++ because it isn’t a command or library it is as simple as reading a text file.
In the shown figure, each row represents the record of an employee where each value separated by the comma “,” is a field or the characteristic of the employee. The file name displayed at the top of the figure is “Employee.csv”, where “Employee” is with the extension “.csv”.
Example # 01:
In this example, we will implement a code in which we will first create a new file and then read that file. The file will have an employee’s data including the name of the employee, the department, and the salary of the employee. Let us now move to our code, after including our header file iostream which is used to enable the compiler to perform input-output operations. Then include the fstream that enables us to work with files, like creating modifying, deleting, etc, and the third library that we included is string library that enables us working with the strings or collection of characters.
We move into our main function where we will create a file for storing employee data. Let’s first declare the fstream variable “myfile” which we will use to work with the file. In the next step, we used myfile.open which is used to open an existing file or to create a new file to this open() statement. We will pass the name of the file and the ios::out argument which allows us to write into the file.
After opening the file, we will pass the values to the values using the “myfile” variable. In this, the “myfile” variable will be used to write the given values into the CSV file. We have passed 3 employee’s data naming “john smith”, “Rick Adson” and “Victoria William”. Against with that, their department and salaries are also passed. After writing all the values into the file, we will close the file successfully using the close() keyword.
#include<fstream>
#include<string>
using namespace std;
int main() {
fstream myfile;
myfile.open("Employee.csv", ios::out);
myfile<<"Name"<<","<<"department"<<","<<"salary"<<"\n";
myfile<<"Jhone smith"<<","<<"Information technology"<<","<<"$122"<<"\n";
myfile<<"Jhone smith"<<","<<"Information technology"<<","<<"$122"<<"\n";
myfile<<"Jhone smith"<<","<<"Information technology"<<","<<"$122"<<"\n";
myfile.close();
After creating the CSV file, we will now move towards our reading process in which we have declared two variables. The first one will be the fstream variable that will handle the file opening and reading functions and the second one will be the string variable that will be responsible for holding the value that is parsed from the file by the variable “myfile2”, the string variables in this example is “details”.
After the declaration of the variable, we simply displayed the text for the success of file creation to check whether the code is working or not. Now, using the new fstream variable “myfile2” we will open the file “employee.csv” using an open statement to which we passed the file name that we want to read and the “ios::in:” keyword that is responsible for opening the file as read mode. One thing to keep in mind for the read method, you cannot use or define the same fstream variable because it will display an error that the variable already exists.
In our next step, we defined for loop that will execute four times. It will start from index zero to index three which means all four rows will be printed. And each value will be separated by the delimiter comma. Within the for loop, we call a “getline()” function which is the built-in library provided by C++ to get the values from the specified file or memory. To the getline function, we passed two variables the “myfile2” and the second “details” variable. Myfile2 variable reads the values stored in the file one by one and passes them to the details variable. Using the “cout” statement, we display the value that is stored in the “details” variable. At the end of the code, the null value is returned.
string details;
cout<<"\nCsv File created!";
cout<<"\n\nDisplaying the content of CSV file\n\n";
myfile2.open("Employee.csv", ios::in);
for(int i=0; i<=2; i++) {
getline(myfile2,details);
cout<<details<<"\n";
}
return 0;
}
As shown in the snippet below, the “Employee.csv” file contains 4 rows. The first row is the label defining the fields, displaying the name, department, and salary of the employee. Each field in the row is separated using a comma where each record is separated via a new line command “/n”.
Let us figure out the output we obtained after running the read code. In the screenshot below, the content of the file “employee.csv” is displayed. All the rows of the file are executed with the separation of values using comma “,”.
Conclusion
In this guide, we have covered the topic of how to read a CSV file in C++. There is no built-in functionality in C++ to read or write CSV files but we can use its getline, fstream, string libraries to perform write or read operations on CSV files. C++ supports working with every type of filling system as well as CSV files. It is the best way to read a large amount of data by simple code that we performed in our example.