How to Read and Parse CSV Files in C++?

How to Read and Parse CSV Files in C++?

How hard can it be to read a CSV file in C++? The wrong way will lead you to errors, messy output, or even crashing your program. This article covers how to do so using C++ (read and parse CSV files), delimiter handling, how to keep the data in vectors, common pitfalls, and their solutions, with easy-to-read and optimized code. Let’s dive in!

Table of Contents:

What Does It Mean to Read and Parse CSV Files

Reading a CSV File

In C++, the meaning of reading a CSV(Comma-separated values) file is to open the file. After opening the file, read the content that consists of the file line by line and extract the row’s data. By using fstream, the file reads the file streams(ifstream) from the <fstream> library.

Parsing a CSV File

The meaning of parsing a CSV file is to extract the individual values from each row by splitting the line based on the delimiter(e.g., comma(,)). The extracted values are processed and stored for further use.

Methods to Read and Parse the CSV Files in C++

The two common methods to read and parse the CSV files in C++ are:

Method 1: Using ifstream and getline

In C++, using ifstream and getline is the simplest approach, where we read the file line by line and split the line by using delimiter.

Steps to Make It Work:

  1. Create a CSV File
    • Name the file as data.csv and place it in the same directory
    • Example for the data.csv file
    • Name, Age, City
    • Arjun, 25, Hyderabad
    • Arya, 30, Kolkata
    • Sathvik, 22, Guntur
  1. Compile and Run the C++ code
    • For Linux/macOS (GCC):
      g++ read_csv.cpp -o read_csv
      ./read_csv
    • For Window(MinGW):
    • g++ read_csv.cpp -o read_csv.exe
    • read_csv.exe

Now run the code to read and parse a CSV file.

Example:

Cpp

Output:

Using ifstream and getline Output

The output for this program depends on the data.csv content

Example:

If the data.csv contains:

Name, Age, City
Arjun, 25, Hyderabad
Arya, 30, Kolkata
Sathvik, 22, Guntur

Then the output will be:

Name | Age | City |
Arjun | 25 | Hyderabad
Arya | 30 | Kolkata
Sathvik | 22 | Guntur

Explanation: The above program opens the file called data.csv for reading by using std::getline(). The file is processed line by line. Assigning std::stringstream to convert each line and split the values based on the comma(,) delimiter. The values are stored by using std::vector<std::string>. At last, the extracted values are printed with a | separator.

Method 2: Storing the Parsed Data in a Vector

In some of the cases, for storing the CSV files in a structured format like a 2D vector.

Example:

Cpp

Output:

Storing the Parsed Data in a Vector Output

The output for this program depends on the data.csv content

Example:

If the data.csv contains:
Name, Age, City
Arjun, 25, Hyderabad
Arya, 30, Kolkata
Sathvik, 22, Guntur

Then the output will be:

Name | Age | City
Arjun | 25 | Hyderabad
Arya | 30 | Kolkata
Sathvik | 22 | Guntur

Explanation: The above program reads a CSV file line by line and extracts each row’s data. Using a string stream, the extracted row’s data splits the line into individual values and stores them in a 2D vector as std::vector<std::vector<std::string>>. At last, it prints the stored data, row by row, with delimiter separation.

Steps To Get The Desired Output

1. Ensure the CSV File Exists

Make sure the data.csv file is the main.csv file in the same directory as your C++ source(main.cpp). The program will write “Error opening file!” if the file does not exist or is named differently.

2. Compilation Steps

Method 1: Compiling your program using g++

1. If you are using g++ under a terminal or command prompt, compile with:

g++ main.cpp -o csv_reader

2. Run the executable:

./csv_reader

Method 2: Microsoft Visual Studio (MSVC)

1. For MSVC in Windows, compile the program with:

cl main.cpp

2. Run the generated executable:

main.exe

Note: If you are using an online compiler, it must have file-handling support and file upload features for data.csv. Certain online compilers deny access to files.

3. Debugging “Error opening file!” Issue

If you still get “Error opening file!, check the following:

  • Ensure data.csv, which is in the same directory as our compiled program.
  • Verify that data.csv has read permissions (chmod 644 data.csv on Linux/macOS).
  • Maybe try an absolute path in readCSV(), e.g.:
std::vector<std::vector<std::string>> csvData = readCSV("C:/path/to/data.csv");

At this point, you can run and compile your program.

Handling CSV with Different Delimiters

Instead of a comma(,) some CSV files use a semicolon (;) or tab(\t).

Example for semicolon-separated CSV:

std::getline(ss, cell, ';'); // Use semicolon as a delimiter

Best Practices for Reading and Parsing CSV Files

  1. You should always check if the file is opened successfully using if (! file.is_open()).
  2. Some CSVs use semi-colon (;) or (\t) instead of comma(,).
  3. Leading and trailing spaces are removed by std::ws.
  4. Assign a default value if the cell is empty.
  5. For converting to int and float, use std::stoi(cell) or std::stod(cell).

Use Cases of Reading and Parsing the CSV Files

  1. CSV files are one of the most common file types used in data science and analytics.
  2. There are settings of many applications to store in CSV files.
  3. CSV files are used for the import/export of database data.
  4. CSV data can be converted to a human-readable report.
  5. System logs, experimentations, and transaction histories are generally saved in a CSV format.

Conclusion

The fundamental task of data processing in C++ involves reading and parsing CSV files. With ifstream and stringstream, it is possible to extract and manipulate CSV data efficiently. These best practices help the order path of different file formats and avoid pitfalls.

How to Read and Parse CSV Files in C++? – FAQs

1. What is a CSV file?

A CSV (Comma-Separated Values) file is a file format that contains tabular data in plain text format and separates the values with the comma.

2. Reading CSV File in C++ with Examples

Make use of std::ifstream to open the file, while using std::getline() in combination with std::stringstream to parse through each line.

3. What do we do if we have missing values in a CSV file?

Check if a cell is empty while parsing, in case the cell is empty, replace it with a default value.

4. What kind of library is required to parse CSV in C++?

Basic CSV parsing can be achieved using standard C++ libraries such as <fstream>, <sstream>, and <vector>.

5. How would you compile a C++ program that reads CSVs?

Use g++ filename. cpp -o outputfile and run outputfile (Linux/macOS) or outputfile.exe (Windows).

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner