-1

There are many variations of this question on this site but all of the answers assume the user has a good fundamental knowledge of how C++ works. As a beginner, this is not good for me.

I have a very simple subroutine attempting to print a single item from a csv file.

void parseCSV() 
{
    int line = 0;
    ofstream myFile("C:/Users/joe-p/Documents/Book1.csv");
    getline(myFile, line, ',');
}

"Getline" is tagged with an error: "No instance of everloaded function "Getline" matches the argument list"

THis is very frustrating as i know the answer is in multiple answers which i've read already, but copying the example code and just trying to cause myself just isn't working for me.

Joe Pringle
  • 51
  • 1
  • 8

3 Answers3

2

That's not how std::getline works.

You need to read as strings and then convert the strings into integers.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

ofstream myFile("C:/Users/joe-p/Documents/Book1.csv"); is an output stream, to call getline you need to use and input stream.

Manuel
  • 2,526
  • 1
  • 13
  • 17
2

Problem:

  1. std::getline first argument should be an input stream. std::ofstream is an output stream.
  2. std::getline takes a std::string& as second parameter, not an int.

Solution:

You should probably open your file as an std::ifstream and use either the std::ifstream::operator>> or std::getline to fill a std::string an then parse it to an int.

Additional information:

  1. using namespace std; is considered a bad practice (More info here).

Full code:

void parseCSV() 
{
    std::string line;
    ifstream myFile("C:/Users/joe-p/Documents/Book1.csv");
    getline(myFile, line, ',');
    int num = std::stoi(line);
    std::cout << num << "\n";
}
Gary Strivin'
  • 908
  • 7
  • 20
  • Very useful information, thank you! although, that snippet of code on it's own doesn't print any data and i can't figure out what i have to change to make that happen. What modifications would i have to make to it to print the item in row 1, column 1? If i get an example of that i should be able to work out how to manipulate it from there. – Joe Pringle Dec 22 '20 at 05:37