I want to parse the following text from a file example_file.txt
# Number of busStop
3
# Id Name Lat Lon
1 Cadorna 450781 76761
using the following example code:
int main() {
int _numberBus;
ifstream file;
file.open("example_file.txt");
if (file.fail())
throw runtime_error("Something goes wrong");
string line;
getline(file, line); // Skip Comment Line
getline(file, line);
istringstream converter; //??
converter.str(line); //??
converter >> _numberBus; //??
}
I am used to reading files in C, but now in C++ this is kind of confusing. I don't understand well the last three lines. Why do I need that so call "converter" object from the istringstream class? My professor said if I recall well that str converts the line to a string. If it was already a string before that, why is it necessary? Why can't I just skip the last three lines and do "_numberBus = (int)line" ?