I'm trying to read a textfile which looks like this:
Gate
People
Crab
Motorbike
My code is:
string line;
vector<string> v_names;
ifstream myfile("c:/temp/test.txt");
if (! myfile.is_open()) {
cout << "Failed to open" << endl;
}
else {
cout << "Opened OK" << endl;
}
myfile.unsetf(ios_base::skipws);
unsigned line_count = count(istreambuf_iterator<char>(myfile), istreambuf_iterator<char>(), '\n');
while (getline(myfile, line)){
v_names.push_back(line);
}
If I want to get the size of my vector with v_names.size()
it gives back 0. If I call v_names[0]
I get the error "Vector subscript out of range"
What am I doing wrong?