0

I have a csv file called "input.csv" (UTF-8, no CR, LF only). It looks like

file data

it has 51 rows and 3 columns. I have to read the csv file and perform some mathematical operations on the data. I have tried couple of codes to read the csv file in c++ but none of them seem to work. I tried these link

  1. How to read a csv file data into an array?,
  2. How can I read and parse CSV files in C++?

I am using these codes and changing the file name but I am not getting any outputs. I cannot seem to figure out the problem. currently I am using this code taken from second link

#include <iterator>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>

class CSVRow
{
    public:
        std::string_view operator[](std::size_t index) const
        {
            return std::string_view(&m_line[m_data[index] + 1], m_data[index + 1] -  (m_data[index] + 1));
        }
        std::size_t size() const
        {
            return m_data.size() - 1;
        }
        void readNextRow(std::istream& str)
        {
            std::getline(str, m_line);

            m_data.clear();
            m_data.emplace_back(-1);
            std::string::size_type pos = 0;
            while((pos = m_line.find(',', pos)) != std::string::npos)
            {
                m_data.emplace_back(pos);
                ++pos;
            }
            // This checks for a trailing comma with no data after it.
            pos   = m_line.size();
            m_data.emplace_back(pos);
        }
    private:
        std::string         m_line;
        std::vector<int>    m_data;
};

std::istream& operator>>(std::istream& str, CSVRow& data)
{
    data.readNextRow(str);
    return str;
}   
int main()
{
    std::ifstream       file("input.csv");

    CSVRow              row;
    while(file >> row)
    {
        std::cout << "4th Element(" << row[3] << ")\n";
    }
} 

Does anyone knows where is the problem?

2
  • When you step through the program with the debugger (and set it up in VS Code if you haven't already. Using a debugger to help you debug can save you insane amounts of time) Is input.csv successfully opened? Commented Jul 26, 2021 at 22:06
  • m_data.emplace_back(pos); appears to be strong the location of the comma, not the value found between the commas. Commented Jul 26, 2021 at 22:22

1 Answer 1

2

The first issue is that you are reading floating point numbers into integer vector, so that is going to lose information.

    // std::vector<int>    m_data;   // Don't need this if you know
                                     // there is always three values.
    // std::string         m_line;   // Don't need this.

    // Have a member for each column
    double col1;
    double col2;
    double col3;

Your second issue is that the code above is used to parse strings. You need to convert that into reading floating point numbers.

    void readNextRow(std::istream& str)
    {
        std::string line;
        std::getline(str, line);

        std::stringstream linestream(line);
        char comma1;
        char comma2;
        bool ok = false;

        if (linestream >> col1 >> comma1 >> col2 >> comma2 >> col3) {
            // Read worked.
            if (comma1 == ',' && comma2 == ',') {
               // There were commas
               ok = true;
            }
        }
      
        if (!ok) {
            str.setstate(std::ios::failbit);
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.