If you are an engineer used to coding in Python or Matlab who is transitioning to C++, you will soon find out that even the most innocent task will now require several lines of code. A previous post has already shown how to export data to a CSV file. In order to facilitate your transition to C++, see below for an example of how to read your new CSV file.
Utils.cpp
#include <string> #include <vector> #include <sstream> //istringstream #include <iostream> // cout #include <fstream> // ifstream using namespace std; /** * Reads csv file into table, exported as a vector of vector of doubles. * @param inputFileName input file name (full path). * @return data as vector of vector of doubles. */ vector<vector<double>> parse2DCsvFile(string inputFileName) { vector<vector<double> > data; ifstream inputFile(inputFileName); int l = 0; while (inputFile) { l++; string s; if (!getline(inputFile, s)) break; if (s[0] != '#') { istringstream ss(s); vector<double> record; while (ss) { string line; if (!getline(ss, line, ',')) break; try { record.push_back(stof(line)); } catch (const std::invalid_argument e) { cout << "NaN found in file " << inputFileName << " line " << l << endl; e.what(); } } data.push_back(record); } } if (!inputFile.eof()) { cerr << "Could not read file " << inputFileName << "\n"; __throw_invalid_argument("File not found."); } return data; } int main() { vector<vector<double>> data = parse2DCsvFile("test.csv"); for (auto l : data) { for (auto x : l) cout << x << " "; cout << endl; } return 0; }
Your code crashes
C:\Users\LindelaniZ\Desktop\CPlusToCSV\main.cpp|17|error: no matching function for call to ‘std::basic_ifstream::basic_ifstream(std::string&)’|
what commands are you using to compile your code? I am using g++ -std=C++11 utils.cpp -o utils and I am getting an “expected unqualified-id before e” in the catch function.
Here’s what I’m using to compile the code and what I get in my terminal:
bernardoct@DESKTOP-J6145HK ~
$ g++ -std=c++11 test.cpp -o test
bernardoct@DESKTOP-J6145HK ~
$ ./test
0 1 2
3.5 4 5
bernardoct@DESKTOP-J6145HK ~
$ cat test.csv
0,1,2.0
3.5,4,5
bernardoct@DESKTOP-J6145HK ~
$ g++ –version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Maybe if you use -std=c++11 instead of -std=C++11 (lower case “c” instead of caps) it will work?
Dear sir,
I’m getting this error:
csvfile1.cpp:36:52: error: expected unqualified-id before ‘e’
catch (const std::invalid_argument e) {
^
csvfile1.cpp:36:52: error: expected ‘)’ before ‘e’
csvfile1.cpp:36:52: error: expected ‘{’ before ‘e’
csvfile1.cpp:36:52: error: ‘e’ was not declared in this scope
compilation terminated due to -fmax-errors=4.
make: *** [csvfile1_x] Error 1
please help me.
Thank you
Hi Devi. Sorry about the delay. To be honest, I’m not sure about what’s happening. Are you compiling the code with gcc or g++? When I compile and run it, here’s the output I get:
bernardo@bernardo-Precision-T3610 ~
$ ls test*
test test.c test.csv
bernardo@bernardo-Precision-T3610 ~
$ g++ -o test test.c
bernardo@bernardo-Precision-T3610 ~
$ ./test
0 1.1 2
3 5.2 3
bernardo@bernardo-Precision-T3610 ~
$ cat test.csv
0,1.1,2
3.0,5.2,3