How to create a CSV file in C++

In this tutorial, we will learn how to create a CSV file in C++.

What is a CSV file?

  • It is a plain text file that stores data.
  • Each row of this file contains data separated by a comma.
  • It helps in exchanging data between different applications.
  • The extension of a CSV file is .csv .

Structure of a CSV file

It has a very simple structure. It just contains rows of data separated by commas.




For ex.

C++ csv data

The above data will be represented in a CSV file as follows:

How to create a CSV file in C++

Now, let’s see the program to create a CSV file.

Program to create a CSV file in C++

#include<iostream>
#include<fstream>   //used for file handling
#include<string>    //used for strings
using namespace std;

/*Everytime before running this program delete previously created 'Student_data.csv' file to avoid buffer overflow errors*/

int main()
{
    int roll_no;
    string name,college,class_of_student;
    
    fstream fout;
    
    // opening an existing csv file or creating a new csv file
    fout.open("Student_data.csv", ios::out);
    fout<<"Name"<<","<<"College"<<","<<"Class"<<","<<"Roll no"<<"\n";
    
    cout<<"Enter details of 5 students=>";
    for(int i=1; i<=5; i++)
    {
        cout<<"\nName of student "<<i<<" :";
        getline(cin,name);
        cout<<"College of student "<<i<<" :";
        getline(cin,college);
        cout<<"Class of student "<<i<<" :";
        getline(cin,class_of_student);
        cout<<"Roll no of student "<<i<<" :";
        cin>>roll_no;
        cin.ignore();   /* if we use getline fun after cin function then it will skip 1 input. To avoid this we need to use 'cin.ignore()' fun to flush newline out of the buffer*/
    
        fout<<name<<","<<college<<","<<class_of_student<<","<<roll_no<<"\n";    // writing data to a csv file
    }
    fout.close();   // closing csv file

    fstream fin;
    string data;
    cout<<"\nCSV file has been successfully created..!!";
    cout<<"\n\nDisplaying the content of CSV file=>\n\n";
    
    // opening existing csv file in read mode        
    fin.open("Student_data.csv", ios::in);
    
    for(int i=0; i<=5; i++)
    {
        // getline fun will read one entire row of csv file and will store it in string 'data'
        getline(fin,data);
        cout<<data<<"\n";
    }
    
    return 0;
}

Input/Output:

Enter details of 5 students=>                                                                                                    
Name of student 1 :Virat                                                                                                         
College of student 1 :abc college                                                                                                
Class of student 1 :BE-2                                                                                                         
Roll no of student 1 :18                                                                                                         

Name of student 2 :Rohit                                                                                                         
College of student 2 :xyz college                                                                                                
Class of student 2 :TE-1                                                                                                         
Roll no of student 2 :45                                                                                                         

Name of student 3 :Hardik                                                                                                        
College of student 3 :pqr college
Class of student 3 :TE-3                                                                                                         
Roll no of student 3 :33                                                                                                         

Name of student 4 :Shikhar                                                                                                       
College of student 4 :abc college                                                                                                
Class of student 4 :BE-1                                                                                                         
Roll no of student 4 :25                                                                                                         

Name of student 5 :Ms                                                                                                            
College of student 5 :pqr college                                                                                                
Class of student 5 :BE-2                                                                                                         
Roll no of student 5 :7                                                                                                          

CSV file has been successfully created..!!                                                                                       

Displaying the content of CSV file=>

Name,College,Class,Roll no                                                                        
Virat,abc college,BE-2,18                                                                                                        
Rohit,xyz college,TE-1,45                                                                                                        
Hardik,pqr college,TE-3,33                                                                                                       
Shikhar,abc college,BE-1,25                                                                                                      
Ms,pqr college,BE-2,7

Time Complexity

O(n), Where n is a no. of rows of data.

You may also read:

  1. Find determinant of a matrix in C++
  2. Relative Sorting Algorithm and Implementation in C++

 


Leave a Reply

Your email address will not be published. Required fields are marked *