1

I have a csv file with an 5 columns and 100 rows. My objective is to load the file into a vectorData

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

using namespace std; 

int main()
{

int count = 0;
vector<double> data;
string line;

cout << "Testing loading of file." << endl;
ifstream myfile ("iris.csv");
if ( myfile.is_open() )
{
     while ( ! myfile.eof() )
     {
           getline (myfile, line);
           data.push_back(line);
      // logs.at(count) = line;
           count++;
     }
     myfile.close();
}else{
      cout << "Unable to open file." << endl;
}
cout << "the log count is: " << count << endl;

return 0;
}

I tried writing the above code to just enter 1 value into the vector but when I try to compile i get errors

lab6.cpp: In function ‘int main()’:
lab6.cpp:22:35: error: no matching function for call to                        ‘std::vector<double>::push_back(std::__cxx11::string&)’
            data.push_back(line);
                               ^
In file included from /usr/include/c++/6.3.1/vector:64:0,
                 from lab6.cpp:4:
/usr/include/c++/6.3.1/bits/stl_vector.h:914:7: note: candidate: void             std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = double; _Alloc = std::allocator<double>; std::vector<_Tp, _Alloc>::value_type = double]
   push_back(const value_type& __x)
   ^~~~~~~~~
/usr/include/c++/6.3.1/bits/stl_vector.h:914:7: note:   no known     conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const value_type& {aka const double&}’
/usr/include/c++/6.3.1/bits/stl_vector.h:932:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = double; _Alloc = std::allocator<double>; std::vector<_Tp, _Alloc>::value_type = double]
   push_back(value_type&& __x)
   ^~~~~~~~~
/usr/include/c++/6.3.1/bits/stl_vector.h:932:7: note:   no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘std::vector<double>::value_type&& {aka double&&}’

Can someone point me in the right direction Of how i can modify the code or start from scratch in order to load values into a 2d Vector?

sample data from csv file.

-0.57815,0.83762,-1.0079,-1.0369,-1
-0.88983,-0.20679,-1.0079,-1.0369,-1
-1.2015,0.21097,-1.0769,-1.0369,-1
-1.3573,0.0020888,-0.93891,-1.0369,-1
-0.73399,1.0465,-1.0079,-1.0369,-1
-0.11064,1.6731,-0.80094,-0.683,-1
-1.3573,0.62874,-1.0079,-0.85994,-1
  • In addition, `vector data;` is not a 2D vector. `vector> data;` is probably as close as you need, but can be slow due to poor spatial locality. If you always have 5 columns `vector` should be faster. – user4581301 Mar 20 '17 at 23:01
  • `while ( ! myfile.eof() )` is a common error. More on that here: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – user4581301 Mar 20 '17 at 23:02
  • and there is no point to `count++;`. `vector` knows how big it is. `cout << "the log count is: " << data.size() << endl;` will do what you need. – user4581301 Mar 20 '17 at 23:03
  • Thanks for the help can you point me to the duplicate question I thought I had looked but didn't find anything. – user1251268 Mar 21 '17 at 15:04
  • It's linked in the hold notice above the question. Duplicated here for convenience: http://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c The answers present several different ways to read simple CSVs. In your case the first answer's first option should be good. For convenience, here is a link to documentation on the `std::getline` function: http://en.cppreference.com/w/cpp/string/basic_string/getline – user4581301 Mar 21 '17 at 16:52
  • @user4581301 the answer you are proposing reads into a vector of strings not a 2d vector - present a generic answer is not good enough, there may be better ways to implement a solution specific to this question – Willeman Oct 03 '17 at 07:17
  • @Willeman The first answer to the question provides a method for reading a single line. The leap to multiple lines is trivial: Call the function multiple times and store the returned `vector`s in another `vector`. The leap from `string` to `double` is also trivial: Call `std::stod`. There is likely a more optimized solution, but first test that you need one. – user4581301 Oct 03 '17 at 17:24

1 Answers1

-1

There are at least 2 issues here:

  1. You are trying to push a whole line (which in the case of your file consists of multiple numbers) into the vector. So you also need to split the line by commas.

    I would use strtok to split up the string.

  2. vector.push_back() expects an argument of type double, but you are passing it a string. You must first convert your string into a double.

    The first method that comes to my mind for converting strings to doubles is atof (which actually converts a c_string to a float, but hopefully you can handle the conversion of a string to a c_string and a float to a double.

    Some people really hate atof due to some safety issues and they aren't entirely wrong, but its likely all you need for your program.

For both of these issues, you shouldn't have any trouble searching stack overflow to find examples of how to do them.

Also, I noticed that your example file looks like each column indicates a different type of data. Most likely, you don't actually want to store those in a vector, but rather some 2 dimensional object (such as vector>).

NateW
  • 908
  • 1
  • 8
  • 28