C++

C++ Write CSV File

Before diving deep into our topic, let us know about the CSV files. CSV is the comma-separated value. All the values in that file are separated using commas which indicates that this kind of file stores the data in a tabular form. The CSV files are used to store data related to some object. Suppose we have information about an employee that will include name, salary, department, etc. Every row denotes the number of records, and each record consists of one or more fields that are separated by commas. Lines are the records of objects where each record contains multiple columns. It has an equal number of commas because it is a tabular form. CSV files are used in data exchange files. We know there is no specific method or keyword to write into a CSV file. If we want to write into a CSV file, we have to use the file I/O datatypes, and logic-based coding will be performed to write into a CSV file.

Syntax

There is no such specific syntax for writing into CSV files.

This is a sample of how the CSV files look. All the values in it are separated using commas “,”, and each row indicates the characteristics of a single object. In the previous snippet, we can see the employee data is stored, and each row contains one employee’s details, where each column indicates the number of characteristics.

Example # 01

Let us start by creating a simple CSV file. Creating a CSV file is similar to creating a text file. For example, getting data from the user and writing it to the file using commas “,” as a delimiter, and “/n” as a row separator. In this example, we will create a CSV file in which we will store three records of students with attributes “name”, “id”, “class”, and “college name”. These records are taken from the user as input. Let’s now look at our code, which is shown in the following figure.

After including our header files, iostream, fstream, and string, we dive into our main function, where our whole process is done. The fstream is the header file used to perform input/output operations associated with the file system. Inside the main function, we declare one integer variable, “std_id”, which will be responsible for holding the student’s roll number that would be an integer value. And then, we declared 3 string variables “name” as the name of the variable reflects the value that it will contain is the student’s name. The second one is the “college” that will hold the institute’s name, where the third variable, “std_class,” is responsible for holding the student’s class.

After declaring all our variables, we declared “fout” using fstream, which is the datatype that denotes the file stream and allows us to perform read, write, and update operations on files. Now, using the file stream variable “fout,” we will open the file named “std_details.csv” file using the statement open() to which we passed our file name with the keyword “ios::out” that is used to open the file for writing purposes. As we have to get values from the user as input, we will ask the user to enter the values stored in a file using fout. We first wrote the first row, which will tell us what is stored in the CSV file, and then we asked the user using a cout statement to enter the values. Now, we have declared for a loop of size 3 which means the user has to enter the record of three students. Step-by-step, we will get all of the values from the user to store in one row, and each value is separated using the delimiter comma “,”.

To get the values from the user, we used the getline() statement, passing two arguments. The first one is “cin”, which tells the compiler to take the value entered by the user as input and then store it in the variable being passed as the second argument. After clearing the buffer using the cin.ignore() statement, we simply displayed the record entered once, and then the pointer moved out of the loop. The loop will get the values and print them until the iterations are completed.

Using “fout”, we displayed the records that the user entered and each record is separated using a comma “,”. Moving out of the loop, we will close the file using the fout.close() statement.

Now proceed toward our next step in which we create another fstream variable, “fin”, which we will use to display the data of the file that has been created. Then, we declare a string variable that will store the data parsed from the CSV file. Next, using the new fstream variable “fin”, we will open the file “std_details.csv” using an open statement to which this time we passed the file name that is being created and the “ios::in:” keyword that is responsible for opening the file as read mode.

Next, we have again declared for loop to get the record of the student from the file that will execute three times. Within the for loop, we used the getline() statement, to which two arguments are passed. The first one is the “fin”, and the second one is “data”, the “fin” will be reading the record one by one and then passing it to the variable “data”. Using the “cout” statement, we displayed the records one by one, and at the end, we placed “\n”, which will break the line as the one row is completed.

Let’s look at the created CSV file, as shown in the following CSV file that was automatically displayed as output when the whole code was compiled. We have created a file with the records of three students. Each record is separated using the comma, and each row is split as the record is completed.

As shown in the following figure is the output of our code which displays the data one by one we entered, and at the end, it was displayed in the form of a CSV file.

Conclusion

This article briefly discussed writing into a CSV file and how we can create a CSV file in C++. C++ does not provide any inbuilt library to write into CSV files, whereas we can write the same as the text file. The files are written row by row and separated using commas. CSV files can also be written via pointers. You can write into a CSV file by any method, whether like a text file or using pointers. We hope that this article will be helpful for you to get a better idea of how to store data in tabular form.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.