can someone show me how to use vector to read .txt files.
can someone show me how to use vector to read .txt files.
The simple way to do this but avoid istream_iterators is to read a line with std::getline into a std::string then simply push_back that string into a std::vector.Do that in a loop until you have nothing left in the file.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Better yet, push back an empty string and getline into that string.
Code:fstream file; string fS; vector<string> vS; file.open("in.txt", ios::in); while(!file.eof()) { getline(file, fS, '\n'); vS.push_back(fS); }
It's not a good thing to use eof() to control a loop.
does the same thing. i think...Code:fstream file; string fS; vector<string> vS; file.open("in.txt", ios::in); while(getline(file, fS, '\n')) { vS.push_back(fS); }
It sure does. And the reason is that eof is not set until you pass the end of file so it is safer to read until you reading fails.
Woop?