Reputation: 11
I am trying to take a simple CSV file, split it line-by-line, and print it to the console. Currently I am getting an error when compiling and want to know if I am missing something obvious.
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
int main(int argc , char** argv) {
std::string line;
std::ifstream infile(argv[1]);
if (infile) {
while (getline(infile, line)) {
std::istringstream ss(line);
std::string token;
while(std::getline(ss, token, ",")) {
std::cout << token << "\n";
}
}
}
infile.close();
return 0;
}
The error I am getting is as follows.
csv.cpp: In function 'int main(int, char**)':
csv.cpp:41:46: error: no matching function for call to 'getline(std::istringstream&,
std::string&, const char [2])'
csv.cpp:41:46: note: candidates are:
In file included from /usr/include/c++/4.7/string:55:0,
from /usr/include/c++/4.7/bits/locale_classes.h:42,
from /usr/include/c++/4.7/bits/ios_base.h:43,
from /usr/include/c++/4.7/ios:43,
from /usr/include/c++/4.7/istream:40,
from /usr/include/c++/4.7/fstream:40,
from csv.cpp:21:
/usr/include/c++/4.7/bits/basic_string.tcc:1070:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
/usr/include/c++/4.7/bits/basic_string.tcc:1070:5: note: template argument deduction/substitution failed:
csv.cpp:41:46: note: deduced conflicting types for parameter '_CharT' ('char' and 'const char*')
In file included from /usr/include/c++/4.7/string:54:0,
from /usr/include/c++/4.7/bits/locale_classes.h:42,
from /usr/include/c++/4.7/bits/ios_base.h:43,
from /usr/include/c++/4.7/ios:43,
from /usr/include/c++/4.7/istream:40,
from /usr/include/c++/4.7/fstream:40,
from csv.cpp:21:
/usr/include/c++/4.7/bits/basic_string.h:2792:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.7/bits/basic_string.h:2792:5: note: template argument deduction/substitution failed:
csv.cpp:41:46: note: candidate expects 2 arguments, 3 provided
Upvotes: 1
Views: 450
Reputation: 11791
Perhaps you should use a library for CSV munging... perhaps this question is useful.
Upvotes: 0
Reputation: 52471
The third parameter of getline
is a char
, not a char*
. Make it getline(ss, token, ',')
- note single quotes.
Oh, and beware CSV fields "like"",""this"
(in case you wonder, this is a single field with the value of like","this
). There's more to CSV syntax than meets the eye.
Upvotes: 3