If you are reading whitespace delimited
text, then I wouldn't use get().
This partly depends on the file format and what you want to do with it.
For example, a config or tabular format, I might read into one whole line, using std::getline(istream&, string) and then parse each line into it's constituent parts.
Just for reading a whole load of text though, you could do something like this:
Code:
#include <fstream> // for std::ifstream, std:: ofstream
#include <iostream> // for std::cout
#include <string> // for std::string
#include <vector> // for std::vector
#include <iterator> // for std:: ostream_iterator
#include <algorithm> // for std::copy
using namespace std;
int main()
{
// First write some text - I have put tab delimited line and space demited
// lines in
ofstream ofs("test.txt");
ofs << "Line one using space\nLine\ttwo\tusing\ttab\t\nLine three big spaces no eol";
ofs.close();
// Now open the file
ifstream ifs("test.txt");
// Create a buffer to read the string
string tmp;
// Use a vector to store all the words separately
vector<string> text;
// Just keep reading until the end of the file (or an error!)
// the operator >> will discard anything that it sees as 'whitespace'
// tab's, spaces, newlines
while( ifs >> tmp )
{
text.push_back(tmp);
}
ifs.close();
// This line will just print out every word to stdout
// I put the "#\n" bit, appended after each word, so you can see exactly
// what is in each string (i.e. you will see a '#' at the end of every line)
copy(text.begin(), text.end(), ostream_iterator<string>(cout, "#\n"));
}