Table of Contents
1. Introduction
In the world of data handling and processing, CSV (Comma-Separated Values) files are a staple due to their simplicity and ease of use. They store tabular data (numbers and text) in plain text form, making them compatible with a wide range of applications. In C++, reading a CSV file is a common task, especially when dealing with data processing or analysis.
Consider a CSV file with several columns of data, separated by commas. The goal is to read this file in C++, parse the contents, and perhaps store the data in a suitable data structure for further processing or analysis. The expected output is a successful extraction of data into an accessible format within the C++ program.
2. What is a CSV File?Â
The sample CSV file looks like the one given below:
1 2 3 4 5 6 |
name,gender,age abc,male,21 xyz,female,18 pqr,male,19 |
In this file, each of the values in each line is separated by a comma. The first row represents column names that are name, gender, and age respectively. After each line, there is a newline character that is stored internally and not visible in the file. Each row in the file represents a different record while each comma-separated value represents a field of the record.
3. Using Standard File I/O and String Streams
One of the most straightforward and versatile methods to read a CSV file in C++ is by using standard file I/O (Input/Output) and string streams. This method combines the simplicity of standard file operations with the flexibility of string processing capabilities available in C++.
Here is code to read CSV files using standard file I/O (Input/Output) and string streams:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#include <iostream> #include <fstream> #include <string> #include <vector> #include <sstream> using namespace std; int main() { string fname; cout<<"Enter the file name: "; cin>>fname; vector<vector<string>> content; vector<string> row; string line, word; fstream file (fname, ios::in); if(file.is_open()) { while(getline(file, line)) { row.clear(); stringstream str(line); while(getline(str, word, ',')) row.push_back(word); content.push_back(row); } } else{ cout<<"Could not open the file\n"; } for(int i=0;i<content.size();i++) { for(int j=0;j<content[i].size();j++) { cout<<content[i][j]<<" "; } cout<<"\n"; } return 0; } |
The output of the above code is given below. We have used the CSV file given above as the input to the program.
1 2 3 4 5 6 7 8 |
Enter the file name: sample.csv name gender age abc male 21 xyz female 18 pqr male 19 |
Header Files:
- The program includes necessary header files:
<iostream>
for standard I/O operations,<fstream>
for file operations,<string>
for using strings,<vector>
for using the vector container, and<sstream>
for string stream operations.
using namespace std;
– This line tells the compiler to use the standard namespace, allowing us to writecin
,cout
, etc., instead ofstd::cin
,std::cout
.
- The
main
function is the program’s entry point.
- A string variable
fname
is declared to store the file name. - We prompt the user to enter the file name and read it using
cin
.
vector<vector<string>> content;
– A 2D vectorcontent
is declared to store the lines and cells from the CSV file.vector<string> row;
– A vectorrow
is used to store individual cells of a single line.string line, word;
– Stringsline
andword
are declared for reading each line of the file and each word within the line, respectively.
fstream file (fname, ios::in);
– A file streamfile
is created to read from the file namedfname
.if(file.is_open()) { ... }
– We check if the file is successfully opened.
- Inside the
if
block, awhile
loop reads the file line by line usinggetline(file, line)
. - For each line,
row.clear()
clears therow
vector for new data. - A
stringstream
objectstr
is created with the current line. - An inner
while
loop extracts each word separated by commas usinggetline(str, word, ',')
and adds them torow
. - After processing each line,
row
is added tocontent
.
- If the file does not open, we print an error message
"Could not open the file\n"
.
- Two nested
for
loops iterate overcontent
. - We print each cell followed by a space and print a newline character at the end of each row.
4. Using C-Based Approach
Reading the CSV file in C is fundamentally similar to reading a CSV file in C++. However, we will use a different approach to read the content and display the file on the console output.
Let’ s see the code to read CSV file using C base approach:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <stdio.h> #include <string.h> int main() { char fname[1024]; printf("Enter the file name: "); scanf ("%s", fname); FILE *file = fopen(fname, "r"); if(!file) { printf("Could not open the file\n"); return 0; } char content[1024]; while(fgets(content, 1024, file)) { char *v = strtok(content, ","); while(v) { printf("%s ", v); v = strtok(NULL, ","); } printf("\n"); } fclose(file); return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 |
Enter the file name: sample.csv name gender age abc male 21 xyz female 18 pqr male 19 |
In this example, each row of the CSV file is read and parsed into individual cells, stored in a std::vector<std::string>
, and is ready for further processing as needed by the application. This method is ideal for most common scenarios where CSV files are used for data storage and exchange in C++ applications.
Here is the step by step explanation of the code:
- Including Header Files:
#include <stdio.h>
: Includes the Standard Input and Output Library for functions likeprintf
,scanf
,fgets
, etc.#include <string.h>
: Includes the String Handling Library for thestrtok
function.
- main Function:
- This is the entry point of the program.
- Reading File Name:
char fname[1024];
: Declares a character arrayfname
to store the file name, with a size of 1024 characters.printf("Enter the file name: ");
: Prompts the user to enter the file name.scanf ("%s", fname);
: Reads the file name input by the user into thefname
array.
- Opening the File:
FILE *file = fopen(fname, "r");
: Opens the file with the name stored infname
for reading ("r"
). The file pointerfile
is used to handle the file.
- Checking File Open Status:
if(!file) { ... }
: Checks if the file was successfully opened. If not (!file
), it prints an error message and returns 0, ending the program.
- Reading and Parsing the File:
char content[1024];
: Declares a character arraycontent
to store each line of the file.while(fgets(content, 1024, file)) { ... }
: Awhile
loop that continues as long as there are lines to read from the file.fgets
reads up to 1023 characters from the file into thecontent
array or stops at a newline character or EOF (End Of File).
- Tokenizing Each Line:
char *v = strtok(content, ",");
: Usesstrtok
to split thecontent
string into tokens (words) separated by commas.v
points to the first token.while(v) { ... }
: Awhile
loop that continues as long as there are more tokens (v
is not NULL).- Inside the loop:
printf("%s ", v);
: Prints the current token followed by a space.v = strtok(NULL, ",");
: Callsstrtok
again withNULL
as the first argument to continue tokenizing the same string. It moves to the next token incontent
.
- Printing a Newline Character:
printf("\n");
: After processing each line (all tokens), it prints a newline character to separate the rows of the CSV file.
- Closing the File:
fclose(file);
: Closes the opened file to release resources.
In summary, this program reads a CSV file, tokenizes each line by commas, and prints each token (cell) to the console. The strtok
function is used for splitting the lines into tokens, and fgets
is used for reading lines from the file.
5. Conclusion
Comma Separated Value (CSV) files are used to store tabular data. C++ does not have any inbuilt library to read CSV files directly. However, we can read them like any other file. The files are read line by line and then separated by commas to get each word. In this way, we can read the files in exactly the same way as it is stored in the original spreadsheet or database.
Although C also does not have any built-in library to read the CSV files, we can read the file using file pointers and then read it in the same way as any other file. To get each value, we separate the values by a comma. This is an elegant way to read the values from CSV files which can be used to process the data for further necessities. You can choose any method to read CSV files according to your convenience.