How to Read Csv File in C++

Read CSV file in C++

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:

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:

The output of the above code is given below. We have used the CSV file given above as the input to the program.

Here is step by step explanation of the code:
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.
Namespace Declaration:
  • using namespace std; – This line tells the compiler to use the standard namespace, allowing us to write cin, cout, etc., instead of std::cin, std::cout.
main Function:
  • The main function is the program’s entry point.
File Name Input:
  • 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.
Declaring Data Structures:
  • vector<vector<string>> content; – A 2D vector content is declared to store the lines and cells from the CSV file.
  • vector<string> row; – A vector row is used to store individual cells of a single line.
  • string line, word; – Strings line and word are declared for reading each line of the file and each word within the line, respectively.
Opening the File:
  • fstream file (fname, ios::in); – A file stream file is created to read from the file named fname.
  • if(file.is_open()) { ... } – We check if the file is successfully opened.
Reading and Parsing the File:
  • Inside the if block, a while loop reads the file line by line using getline(file, line).
  • For each line, row.clear() clears the row vector for new data.
  • A stringstream object str is created with the current line.
  • An inner while loop extracts each word separated by commas using getline(str, word, ',') and adds them to row.
  • After processing each line, row is added to content.
Handling File Open Failure:
  • If the file does not open, we print an error message "Could not open the file\n".
Printing the Content:
  • Two nested for loops iterate over content.
  • 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:

Output:

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 like printf, scanf, fgets, etc.
    • #include <string.h>: Includes the String Handling Library for the strtok function.
  • main Function:
    • This is the entry point of the program.
  • Reading File Name:
    • char fname[1024];: Declares a character array fname 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 the fname array.
  • Opening the File:
    • FILE *file = fopen(fname, "r");: Opens the file with the name stored in fname for reading ("r"). The file pointer file 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 array content to store each line of the file.
    • while(fgets(content, 1024, file)) { ... }: A while loop that continues as long as there are lines to read from the file. fgets reads up to 1023 characters from the file into the content array or stops at a newline character or EOF (End Of File).
  • Tokenizing Each Line:
    • char *v = strtok(content, ",");: Uses strtok to split the content string into tokens (words) separated by commas. v points to the first token.
    • while(v) { ... }: A while 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, ",");: Calls strtok again with NULL as the first argument to continue tokenizing the same string. It moves to the next token in content.
  • 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.

Was this post helpful?

Leave a Reply

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